In code, there is another way we can represent the compound boolean expression "Or" -- using nested if/else statements. We will now work through changing our code to use nested if/else statements instead of one if/else with the compound boolean expression.
Let's take a smaller example before changing our car game. Consider that there are two balls under a cup each, one is red and one is blue.
You choose a cup and:
if you chose the red ball the bunny says "Hooray! Red is my favorite color!"
else the bunny says "Darn, I don't like blue.".
We could express this using an if/else statement:
Now we add a third cup with a green ball underneath it. You choose a cup and:
if you chose the red ball the bunny says "Hooray! Red is my favorite color!"
else, if you chose the blue ball the bunny says "Darn, I don't like blue."
else the bunny says "Well, green is ok.".
To express this we need to use more than one if/else statement.
[Q9.3.1]: How do you think we could express this complicated English statement using two if/else statements?
Just like with our compound boolean expression in section 9.2, we want to chunk the complex statement into smaller pieces and then represent those smaller pieces in code:
[if you chose the red ball the bunny says "Hooray! Red is my favorite color!"
else, [if you chose the blue ball the bunny says "Darn, I don't like blue."
else the bunny says "Well, green is ok.".]]
So now our nested if/else statement would look like this:
[Q9.3.2]: If you added a fourth cup with a yellow ball under it, but did not change the code and you chose the yellow ball, what do you think the bunny say? How could you change the code so that the bunny only says something if red, blue or green was chosen?
To do this you would have to change our nested if/else to check for each color and do nothing if none of the colors you wanted (red, blue or green) were chosen:
Now let's go back to our car game example. Remember we have this if/else statement and compound boolean expression:
(click here to see larger image)
Before you modify the world from page 2 save a copy of it as M9-Magnets-pg3-Nested-YourName.a2w
[Q9.3.3]: How many if/else statements do you think you need? Why do we need that many?
Though we want to build up the if/else statements until we have 3 nested statements that look like this:
we should build them up together.
First, delete the else/or/orBoth tile and replace it with checking the parameter magnet against the object magnet:
[Q9.3.4]: What do you think will happen if you press play and click on magnet? magnet2? A tree?
Now let's add another if statement to check if the parameter target is the object magnet2. To do this, we need to add an if/else tile inside the else part of the original if/else tile:
We then need to add the conditional statement to the if/else tile we just added to check if the parameter target if the object magnet2:
Finally, we need to add the code to make the car turn to face magnet2 if magnet2 was clicked on:
[Q9.3.5]: What do you think will happen if you press play and click on magnet? magnet2? A tree?
Now let's add another nested if/else tile to check if magnet3 was clicked on:
And finally, we will add the nested if/else tile to check if magnet4 was clicked on:
Click play. Did that work? Does the car only move to the four magnets?
[Q9.3.6]: What if you wanted the car to turn left 1 revolution if something other than a magnet was clicked on, how would you change the code? Why? (HINT: You only have to add one line of code to your nested if/else)
[Q9.3.7]: Which way of making the car game only work if magnets are clicked on do you think is better: using compound boolean expressions or nested if/else statements? Why?
Remember to save the world form this page as M9-Magnets-pg3-Nested-YourName.a2w