Next we'll explore the code that caused the behaviors we observed in our videos. Even though there were two videos, both were created from the same code -- the only thing that changed was where we placed the Egyptian in the set up of the world.
Download the AngelSometimesMoves world (located at the bottom of the page).
You should note, in this version of the world, the Egyptian is "close to" the Angel (as shown in Video 2 -- the Egyptian is within the blue circle we have added to the world).
Play the world to make sure it does the same thing as video 2 (the Angel should move a comfortable distance to the Egyptian).
Let's look at the code to figure out what is going on, specifically looking at the method EgyptianCallAngel.
Our code is pretty simple. After the Egyptian says "Oh, Angel!" there's a new color tile that we haven't seen before. This green tile is rather large. It is an if/else tile also called an if/else statement.
The first line in the tile always starts with the word if. After that is a mathematical expression that we will use to check the state of the world. In this case we are checking the state of the distance between the Angel and the Egyptian -- specifically checking if the Angel's distance to the Egyptian is less than 5 meters.
In the last module we saw other mathematical expressions. Back then, they always calculated a number (like how far a zombie should move). Here we have a new kind of mathematical expression. It doesn't calculate a number, but it calculates something called a Boolean value. A Boolean value is one of only two possible things: true or false.
This mathematical expression:
evaluates to true in our current world setup. That is, the Angel's distance to the Egyptian is 4 meters. <Extra: Want to know how to get Alice to tell you the exact value of Angel's distance to the Egyptian? Watch this video. Remember you can improve the video quality by pressing the gear-shaped icon at the bottom of the video player and choosing 480p (or higher, if available)>
That is "4 is less than 5". That statement is true. (Remember, the function Angel's distance to Egyptian is calculated and the number it returns essentially replaces those words when the expression is calculated).
[Q8.2.1]: Pick a value to fill in this statement so that it is false: "____ is less than 5"
OK. But what HAPPENS because the state of the world is such that the Angel's distance to the Egyptian is less than 5 meters?
The way that if/else statements work is when the Boolean expression listed after the if evaluates to true, you do the statement (or statements) listed (and indented) under the if. BUT, it does not do any of the statement(s) listed under the Else. Here's a diagram of what statements (code) is executed in this case (e.g. when the Boolean expression is true):
This is exactly what happens when you play the world you downloaded when you started this page.
But what would happen if that Boolean expression didn't evaluate to true?
Let's modify the world and find out. In the world set up (before you hit Play -- not in the code), move the Egyptian back away from the Angel -- far enough that he is no longer standing on the blue circle (by the way, we designed the world so that the circle's edge is exactly 5 meters away from the center of the Angel -- which is at his feet).
Hit Play.
[Q8.2.2]: What happened?
If the Boolean expression evaluates to false (the only other option -- it is NOT true) then it skips over the statement(s) indented under the if and instead does the statement(s), if there are any, under the else.
Below is an image of how the code is executed when the Egyptian starts off "far away" from the Angel -- that is more than (or exactly) 5 meters away from the Angel.
Note that it is possible to have "nothing" happen when the boolean expression is false (that's what we have here -- if the Egyptian is far away he doesn't approach the Angel). The green if/else tile automatically comes with Do Nothing (which you can replace by dragging a method tile over).
[Q8.2.3]: True or False: When the Egyptian is more than 5 meters away from the Angel, the instruction under the if (Angel move) is executed. Why?
[Q8.2.4]: Which of the following statements describes a Boolean situation:
[Q8.2.5]: True or False: The statement that makes the 3D text "The End" visible is only executed sometimes based on how the If/Else statement evaluates.