<< Chapter < Page | Chapter >> Page > |
There is one important stipulation, which I mentioned earlier. The bottom of the forever block is smooth and it is not possible to connect another block to the bottom of the forever block. Therefore, it must always be the last block in a script. Also, a script that ends with a forever block will continue to execute forever unless the program is manually terminated. That may or may not be a good thing.
The code for this program is shown in Image E .
When the user clicks the green flag, the program implements the pseudocode shown in Image F .
The program creates a variable named counter and initializes its value to zero. The code in the if block increments the counter by one during each iteration. For a counter limit value of 20, the code inside the if block is executed 20 times.
When the user clicks the green flag, the first 20 iterations of the forever loop cause the cat to walk about 80-percent of the way around the circumference of a small circle.
Note that the forever loop continues to iterate even after the conditional clause in the if block goes to false. Be sure to click the red stop button in the upper-right corner of the stage toterminate the forever loop. Otherwise, you may find other applications on your computer running very slowly.
Again, because this program is animated, a screen shot of the Stage wouldn't impart much information so I won't show you one. You can copy the code from Image E and run the program yourself. Also, the program has been posted online so that you can run it from there. (See the link to the online version in Resources .)
This program uses a repeat until block to construct a loop structure. This block makes it possible to create a loop that is similar, but differentfrom a do-while loop in Java.
When the user clicks the green flag, the program implements the pseudocode shown in Image G .
The code for this program is shown in Image H .
When the user clicks the green flag, the cat
The cat continues to run in the circle until the user presses the space bar, at which point the repeat until loop terminates.
On my computer, you must hold the space bar down for a short period of time. Simply tapping the space bar doesn't work.
Then the cat moves back to the center facing to the right where it executes a think block, producing the output shown in Image I .
Notification Switch
Would you like to follow the 'Teaching beginners to code' conversation and receive update notifications?