As complex as some of the actions were in our cementTruck example, it was still the same thing done over and over again. In this section we will see how we can put loops together to create even more complex patterns of behavior. Go ahead and download the world we will work with in this section from the bottom of the page: BunnyHop.a2w
Let's review our Discovery video for Nested Counted Loops
[Q10.4.1]: What things do you see that are repeated?
Let's take a look at the following code segment:
[Q10.4.2]: What do you think this code does?
Hmmm... That's not quite what we were aiming for. But let's start with that code, then see if we can fix it. Often time you might have a computing issue that almost works, but not quite. The skills you develop here in observing closely, analyzing detailed things, and making informed guesses and changing things to see if they improve -- these are skills you can use with most every technology "problem".
Go ahead and open the world BunnyHop.a2w world you downloaded from below (if you already haven't). Create the code that I showed above. Drag out a loop (loop 3 times), drag in the 2 HoppingBunny methods (hop and say "Whew!"), and test it out. Doesn't work like in the video? Drat.
Let's see, we want the bunny to hop 3 times THEN (and only after then) should he say Whew.
[Q10.4.3]: So what would happen if you dragged the HoppingBunny say methods out of the loop (off the loop tile)? Try it out. Are we there yet? Describe what you see.
So, somehow we need this entire process -- hop, hop, hop, whew! -- to be repeated 4 times. Sounds like we should use a counted loop -- another counted loop in addition to the one we already have. Drag out another counted loop tile from the bottom. Just drag it to the bottom of your program and choose 4 -- then we'll think about what goes on it to be repeated 4 times.
What do we need to be repeated? Whatever code goes -- hop, hop, hop, whew!. To get the hop, hop, hop repeated -- drag the loop 3 times tile onto the loop 4 times tile. But what we need repeated 4 times is "hop, hop, hop, whew!". So, next drag the HoppingBunny says Whew! tile onto the loop 4 times tile.
[Q10.4.4]: Where on the loop 4 times tile will you place the HoppingBunny say Whew! tile: Will it go above the loop 3 times tile or below it?
Hit Play and see if your code does what our original video showed. Three hops followed by a Whew! with that WHOLE process repeated 3 more (4 total) times.
Sometimes it's really helpful with loops to try to visualize the order in which the computer is executing your loops. Here's a video showing how the computer executes these loops.
(You might need to use fullscreen mode and use the gear icon to change the video quality to read the changing captions)
Now that you have the basic idea of nested loops -- something can be repeated in the inner loop with things repeated less frequently in the outer loop (before or after the stuff in the inner loop) -- there's other things you can try out.
[Q10.4.5]: What would happen if you move the HoppingBunny say Whew! before the loop 3 times tile, but still on the loop 4 times tile?
Try to increase the number of hops between the Whew! -- make it 5, 10! Maybe instead of saying Whew -- he does a spin!
[Q10.4.6]: Can you write code that makes our bunny go:
hop, hop, hop, spin, spin -- repeated 4 times?
Actually you might have thought of one of two ways to get this to happen. If you were thinking of nesting loops, you might have made:
(Note: You could have made the HoppingBunny turn either right or left)
But, since the HoppingBunny turn method takes a parameter -- you might have not used yet another loop -- but instead have set the parameter to HoppingBunny turn to be 2 revolutions.
Now you have a bit of an idea of how all those methods that come with objects in Alice work! A lot of the flexibility provided by the parameters to methods like turn, roll, move, etc. come from loops!
Make the bunny hop 3 times say "Whew", spin 2 times using a loop, and say "I'm getting dizzy". Have the whole thing repeat a large number of times.
Save your world as Ch10-pg4-HoppingBunny-YourName.a2w