5. Building your own if statement in Alice

In learning about the logic of how if/else statements work, we provided you an if/else statement and asked you to modify various parts of it. We hope you understand how it works now.

But the process by which you actually create a new if/else statement in Alice is a bit complicated. It doesn't change anything about how if/else statements work. There's just some things that may seem odd in creating them that we want to show you. It's kind of like the process by which you create a complex mathematical expression in Alice. There are a few steps you go through.

Download the world at the bottom of the page titled "AngelAndEgyptianTaller". You should see an method called WhoIsTaller. Open that method. The method should be empty. We will fill in this method using an if/else statement so that the taller character (the angel or the egyptian) will declare that they are taller than the other character.

Let's get started:

1. Drag the if/else tile from the bottom of the screen to the method area where it currently says do nothing.

2. Choose true from the drop-down menu.

Next, we want to tell the if/else statement to compare the heights of the angel and the egyptian. If the angel is taller than the Egyptian, we want the if statements to execute. If the egyptian is taller than the Angel, we want the else statements to execute. We can do this by seeing if the height of the angel is > the the height of the egyptian.

3. Make sure world is highlighted at the top of the page.

4. Open the functions tab.

5. Look under the Math category and drag the > expression over the if/else area that says true.

6. We will need to give the > expression a default value (but we will change this next to be the Angel and Egyptian's heights). Select 1 from the drop-down menu for the value of a and 1 again for the value of b.

Your code should now look like this:

Now we want to replace the "1" values with the angel's height and the Egyptian's height.

7. Click on the angel.

8. Click on functions.

9. Find the function angel's height and drag it to the first 1 in the if expression (the 1 on the left side of the ">").

10. Now do the same for the egyptian, but instead drag egyptian's height over the other 1 (the 1 on the right side of the ">").

Your code should now look like this:

[Q8.5.1]: If the angel's height is 3 and the egyptian's height is 1, which statements will be performed: The "if" statements or the "else" statements. What if the egyptian's height changed to 4 (and the angel's height is still 3)?

Let's test our code to see if it works. That is, to see if the statements in the if part of the code get executed when the angel's height is greater than the egyptian's and vice versa. To do this, we need to add some statements into the if and else parts of the code (where it currently says do nothing).

We can keep it simple. When the angel is taller, let's make the angel say "I'm the taller one". Similarly, the egyptian can say "I'm the taller one" when it is taller than the angel. Use the angel and egyptian's say methods to add this to your code. When you're done, your code should look like the following:

Now we want to test our program. To make sure the method gets called when we hit play, open up world.my first method (located under world). Drag the WhoIsTaller method into world.my first method. Now the code in WhoIsTaller will get executed when the program is started.

There's one more thing we need to do. We need to know who is taller, the angel or the egyptian, before we run the program so we know what actions we expect to get performed (who will say "I'm the taller one."). Manually adjust the Angel to be much, much taller than the Egyptian. Now press play. Was the correct statement executed (did the angel say it was the taller one)?

Now check to make sure your code works the other way around. Manually adjust the angel to be much, much smaller than the Egyptian. Now hit play. Was the other statement now executed (where the egyptian says it is the taller one)?

Save your world as M8-pg5-WhoIsTaller-YourName.a2w

We have now learned how to modify and if/else statement and confirm that it was working how we expected.

<< Back Next >>