CIS 101
Lab 4.4
LOOPS
In the last lab we saw how to handle a “fork in the road.” In our first two labs, the computer basically followed the instructions we gave it one after another. In lab 4.3 we were able to make the computer go to the left or the right depending on a condition. The control structure that allows for that is a decision . There are many times when we want the computer to repeat a series of instructions. This is called iteration, but a more common term for it is looping. There are basically two kinds of loops based on what keeps the loop going or what eventually causes it to end. There are counting loops that repeat a certain number of times and event controlled loops that keep repeating while a certain condition is true. Actually, both of them depend on a condition much like the if statement, but the condition involves a counter with the counting loops and the event controlled loops don’t. For example, if you are having guests over for dinner, you will set the table for a certain number of people. The process for each place setting is the same, so you count as you go along. When you reach the number of place settings you need, you stop. This would be an example of a count controlled loop. When you go grocery shopping, you keep placing items in your cart until you have everything you need (kind of a loop in itself). When you get to the checkout stand, you take an item out of your basket and place it on the conveyor belt. Then you take another item out, then another, then another. The process of moving each item from the basket to the belt is pretty much the same.
You repeat that process “while it is true” that there are more items in the basket.” Let’s look at an example of both in Alice.
:
1. Start a new world.
2. Add a Gazebo from the buildings gallery to your world. Place it back about half way (far from you).
3. Add a mad scientist from the people gallery. Place him near the front (close to you).
We want him to move closer to the gazebo one meter and jump (move up, move down). We want him to keep doing that until he is within 1 meter of the gazebo, but we don’t know for sure how many meters away from the gazebo he is.
4.
Add one of the scientist’s turn to face methods to the PCE and make him face the gazebo.
5. Drag the While tile from the bottom of the PCE and place it under the turn to face statement.
Select true from the pop-up menu.
6. Drag the scientist's mov e method into the Do nothing part of the while and set it to forward 1 meter .
7. Drag another scientist move method into the while (under the last one) and make him move up 1/2 meter .
8. Drag one more scientist move method into the while loop and make him move down 1/2 meter .
Those last three steps are what we want repeated. That is why we put them IN the while loop block.
Now we will tell Alice how long the scientist should keep doing that.
9. Click the functions tab on the scientist's details.
10. Drag the madScientist is at least threshold away from object tile to where it says true after the word while at the top of the while block.
11. Choose 1 meter and gazebo .
Play your movie. As you can see, he moves and moves and moves and keeps moving while it is true that he is more than 1 meter away from the gazebo.
QUESTION:
What would happen if we didn't turn the scientist BEFORE the while block?
It might be kind of interesting to see how many times the scientist had to move 1 meter to get there.
1. Create a new variable in the PCE called moves and set its initial value to 0.
2. Drag the moves variable to the bottom (but inside) the while block.
3. Choose set value , expressions , moves from the pop-up menus.
Now it wouldn't make much to set a variable's value to the same variable. That is like telling someone to put the pencil in their hand that is already in their hand, but we are not done yet.
4. Pull down the moves menu where it says set value to moves .
5. Choose math and them moves + and 1 from the pop-up menus.
That statement should now look like this:
This is like passing the collection plate around at church. When the plate gets to you it has so much money in it. That is like the reference to moves on the right. You then put in more money, or add to what is already in the plate giving the plate a new value. This line is saying "The plate's ( moves )
New value is what WAS in the plate ( moves ) plus one." A variable can only hold one value at a time, so we are adding one to what is currently in the variable moves and putting this new value back in moves . This is an EXTREMELY important concept and one used often in programming.
To get the scientist to SAY a number, we have to change it into a string.
6. Create a new variable called movesMessage and make sure you choose Other… String from the dialog box that pops up. (We did this in lab 4.2).
7. Drag the movesMessage variable to the PCD under the last statement in the while block.
8. Set its initial value to default string .
9. Click the world on the object tree.
10. Drag the world's what as string tile to that statement we just added and drop it where it says default string .
11. Point to expressions and then click on moves from the pop-up menus.
12. Drag the madScientist's say method to the bottom of the while block, point to expressions and click movesMessage from the pop-up menus.
Play your movie. The scientist should say a number each time he moves and the number should increase by 1 each time.
QUESTION:
What would the moves set value to (moves + 1) message need to say if we wanted the scientist to count by twos?
We are going to make a cow walk in a square. To do that, she has to move forward, turn right 1/4 turn, move forward, turn right 1/4 turn, move forward, turn right 1/4 turn, and move forward, turn right
1/4 turn. Basically, she has to move forward and turn right 1/4 turn 4 times.
1. Start a new world.
2. Click the Add Objects button.
3. Go to the Animals gallery.
4. Click the cow and then click the Add Instance to World button.
5. Drag the Loop tile (it's in the bottom of the PCE) into the Do nothing box of the PCE.
6. Click other… on the pop-up menu and choose 4 .
7. Drag the cow's move tile into the Loop 's Do nothing block and choose forward 1 meter .
8. Add the cow's turn tile into the Loop block and choose right 1/4 turn .
Try your movie.
QUESTION:
What would you change to make the cow go around twice?
One more program. You are going to have to fill in some blanks on this one from what you have learned in this and the previous three labs.
1. Start a new world.
2 Add a bunny (or some other animal or person).
3. Create a variable called number .
4. Create a variable called score and set its initial value to one(1).
5. Create a variable called guess and set its initial value to zero(0).
6. Create a variable called scoreMessage and make it a string.
7. Drag the number variable into the PCE and set its value to 1 using the pop-up menu.
8. Click the world on the object tree and from its functions, find random number.
9. Drag the random number function tile over to replace the 1 that you just assigned to number .
10. Using the more drop down menu in the purple random number tile, set the minimum to 1 and the maximum to 10.
1`. Again, drag the number variable from the variables area down into the PCE and set its value to 1. (I know, this seems to defeat the purpose of what we just did.)
12. From the world's functions, drag the round function over to replace the 1 you just assigned to number .
13. Pull down the 1 drop-down menu from the round function, point to expressions and choose number .
Your first two statement tiles should look like this:
EXPLANATION: We are having the Alice program pick a number from 1 to 10. Computer programming languages usually have some way of generating random numbers. We won't go into any details about how that takes place in the computer. For now, let's just assume that the computer can think.
In Alice, we can tell the random number function what the minimum value should be and what the maximum value should be. UNFORTUNATELY, Alice picks a number with decimal places. That is why we had to add the second line to round what Alice put in the number variable and then put the rounded value back in the number variable.
Here is the rest of our number guessing game.
Have the bunny say "I'm thinking of a number from 1 to 10.
”
Get a number from the user and put it in the guess variable. (You know, like Alice asked for the checkAmount in Alice lab #2.)
(did you notice that big red word?) the user's guess is not equal to the value in number
( != is the "not equal to" sign) do the following steps: tell the user they guessed incorrectly set the value of guess to a number entered by the user. add one to the variable score (HINT: look at the pop-up menu when you drag score down to the PCE)
Then, after the while loop…
Put a message in the scoreMessage variable telling the user how many guess it took them.
(Remember how to join a string like "You took this many guesses" with a number turned into a string?
We did that in lab 4.2
.)
Have the bunny say the scoreMessage string.
WHAT YOU WILL TURN IN:
Your answers to the questions.
A print out of your code for the number guessing game.
Show me your number guessing game on your computer in class.
For three extra points, have the bunny tell them if their guess was too high or too low each time they guess. BE SURE TO WRITE EXTRA ON YOUR PAPER.