Alice+Tut4+If+Else+and+While+and+for+loops


 * ~ home ..... Getting Started ..... ||~ Tutorials and Commands ..... ||~ resources ||

If, Else, and While Loop.

1. Just like our other tutorials, we will start the alice program, by double clicking on it in the applications folder. On start up, cancel the welcome screen. We will start in a default world, and add an object to the world. I will be using a Hare this time around.

2. I will be using a Hare this time around. Click on the add objects button to bring an object of your choice into the world.

3. After you have closed the gallery by clicking the **DONE** button, you can display the methods of your object by clicking the object you just added in the world frame found on the upper left portion of the screen.

4. The properties tab, the one beside methods, shows the different variables available for your object.Variables are used in any programming language to assign values to. as you can see in the image above, the variables available for the object are color, opacity, vehicle, etc.

5.We are going to click on **create new variable** to define a variable to store a number for the object. Let us name the variable myNumber, make it a number type, and assign its initial value to 1.



6. The new variable that you have created after you click OK will show up in properties section of your object's details.



just like our last tutorial, we can use the world’s functions to prompt the user for a number and assign the number to the myNumber variable we just created.

7. Let us first select the myNumber variable and drag it over to the “Do Nothing” area world methods area and select a value of 1. Notice the variable along with its object name is now shown in the first line of the method.



8. To prompt the user for a number entry we click on the world object in the upper left corner and then click on the functions tab in the lower left corner. Use the scroll bar to display the “ask user” related functions. Then drag and drop “ask user for a number” right over the “set value to” drop down and select the string of “Enter a number”.



9. as a note, you should check your progress by pressing the play button that can be found on the top right.

10. Next, we will enter the **if /else** control structure to determine **if** the number the user entered is a negative number. To do this, select the **if/else** control structure at the bottom of the methods area and drag and drop it below the first line of code. **Select true** and several lines of code are entered for you.





11. Recall the logic of an if/else structure and implement the following pseudocode: if myNumber < 0 then Print “You entered a Negative number” else Print “You entered a 0 or a positive number” end if this simply tells the program that if the number you enter is less than zero, the program will tell the user that they entered a negative number, and if the user inputs a number that is 0 or higher, the program will tell the user that they entered a number that is 0 or positive.

12.Using Alice to create this logic is easy. We drag and drop variables and methods in the correct locations. - To enter the myNumber < 0 logic click on the hare object to display the hare’s details. - Then select **properties** and drag and drop **myNumber** right next to the if statement. - When you drop the variable in, you will be given the opportunity to select from several relational operators and values. Select **myNumber < 0.** You may need to select other and then enter 0 if you haven’t assigned 0 before in your session.

13. The results of these actions should look like this:

14. if myNumber < 0 then Print “You entered a Negative number” else Print “You entered a 0 or a positive number” end if We can then use the “hare say” method found in the hare’s details **methods tab** to place the correct logic in the lines. Drag and drop the “hare say” method and assign text as shown below.
 * we are working on the steps of our pseudocode **

15. Do the same thing for the 'ELSE' section, except that the string you will enter is "you have entered a 0, or a positive number"'



16. make sure you check all instances of your if/ else statement by pressing play and using a positive number, a negative number, as well as 0. does the program react as you expect? input = 12

input = -23

input = 0

=**The next part of this tutorial will add a loop structure to what we have already built.**=

17. To add a loop control structure select the **Loop** function at the bottom of the methods area and drag and drop it right beneath the if/else block. When prompted select 5 times to indicate you want the loop to occur exactly 5 times. **Be careful not to drop it in the if/else block area as this would perform different logic than we wanted.**



if done properly, your window should look something like this.

18. Select the hare object to display the hare’s details. Select the **methods tab** to show the available methods. Let us drag and drop the hare “resize” method and the hare ”say” method to the loop structure area. Pick a resize factor of **0.5** and enter the text for the say method as “I think I am shrinking!” 

19. Now it is time to test your creation!!!!!! press play, and let's see what happens. My hare shrunk! if your object got smaller and said it was shrinking, then you have succeeded in this section of the tutorial.

=This section of the Tutorial goes over the creation of a **WHILE LOOP**, which **allows the user to iterate until a certain condition is met,** instead of just incrementing based on a preset number.=

1. Starting from where we left off, select the **while** function and drag and drop it to the methods area right below the loop structure. When prompted to do so, select the true condition.



We are setting up a condition to test. The loop will continue until a specific condition is satisfied. By default, the initial loop will be infinite since the condition is true. We will change that condition in a moment. Once again, be careful to drop it below the loop structure and not inside of it.

2. Before we do too much in the While loop let us resize the hare so we can better visualize his actions in the loop. To do this, drag and drop the hare resize method right above the while true line. Set the resize value to 20.



3. Now the hare will be visible again. We can use a variable to set the while loop exit condition. To do this we click on the **properties** tab and **create a new variable**. Let us call this variable “loopcounter” and set its initial value to 1. When that is completed, your Details should have loopcounter added to it like this.



4. If we want to make the loop iterate 8 times we can use the loopcounter variable and create a logic condition similar to the following pseudocode.

while loopcounter <=8 Do something end while

5. Alice will support this by dragging and dropping the loopcounter into the true condition. Once again, as you drop use the available options to select <= and a value of 8.





6. If this was all entered correctly, it should look like this:



7. Next, we will add some logic in the While loop that will “roll” the hare (using the hare’s “roll” method) and increment the loopcounter. Select the **methods tab** and use the drag and drop feature of Alice to bring the hare’s roll method into the while loop.



This is quite important. If we don’t increment the loopcounter variable, the loop will never exit. When designing while loops, make sure you have programmed a situation where your condition will change in the loop. If not, you will be stuck in the loop forever.

8. the results of all of these actions should look like this.

9.  Finally, we must increment the loopcounter so the while condition will eventually test false and exit the loop. To have this occur, just click on the **properties tab**, drag and drop the loopcounter variable to the while loop right below the hare roll method and select the increment by 1 option.

10. This adds a special line to the code, and once this is completed, play the scene to see what happens.





If your object rolls around after it shrinks and resizes, you got it!

Great Work!