Reeborg lives in Canada where it not only can rain or be sunny, but snow can also be falling ... usually not all three at the same time ... but it does happen... Let’s suppose that only one of those can happen. Then, Reeborg could be faced with the following choices:
if ( it_rains() ) {
play_indoors();
} else if ( it_snows() ){
go_skiing();
} else {
go_swimming(); // assuming it is warm!
}
Notice the use of else for choices 2 and 3, and the additional if statement for the second case. If we took into account other possible weather phenomena, like hail, thunder, fog, drizzle, etc., we could add other choices using additional else if {...} code blocks.
A series of if/else if/ ... /else statements is equivalent to inserting the first code block that evaluates to true. Thus:
if ( false ) {
do_1();
} else if ( true ){
do_2();
} else if ( true ){
do_3();
} else {
do_4();
}
is equivalent to:
do_2();
whereas:
if ( false ) {
do_1();
} else if ( false ){
do_2();
} else if ( false ){
do_3();
} else {
do_4();
}
is equivalent to:
do_4();
etc.
Just two lessons ago, you wrote a program that worked for worlds Hurdles 1 and Hurdles 2 but not for Hurdles 3. Your program was likely something like this:
function jump_over_hurdle() {
// some suitable definition
}
function move_and_jump_until_done () {
move();
if ( at_goal() ) {
done();
}
jump_over_hurdle();
}
repeat(move_and_jump_until_done, 42);
The reason it is not working for Hurdles 3 is that it is written with the assumption that the hurdles are evenly spaced. Let’s use our new condition front_is_clear() and keyword else to change that.
Here’s a new program that should work for the world we mentioned above, provided you fill in the missing pieces.:
function jump_over_hurdle() {
// suitable definition
}
function run_jump_or_finish () {
if ( at_goal() ){
// something
} else if ( front_is_clear() ){
// something
} else {
// something
}
}
repeat(run_jump_or_finish, 42);
Note the structure of the if/else statements; as is mentioned above, you should see that it gives three independent choices: only one of them will be executed.
Do it!
Write such a program and make sure it works.
Could this program work without changing anything for world Hurdles 4? ... Have a look and you will likely conclude that the answer is no. You might want to try it just to be sure. It will take a little while until we are ready to write a program that can make Reeborg race Hurdles 4 as well as the other three.