<< Chapter < Page | Chapter >> Page > |
Because this program is animated, a screen shot of the Stage wouldn't impart much information so I won't show you one. Basically, it would look very similarto Image I without the thought bubble. You can copy the code from Image C and insert it into your own program. Also, the program has been posted online so that youcan run it from there. (See the link to the online version in Resources .)
This program illustrates another usage of a forever block with an embedded if block. The behavior of this program is very similar to a program that I explained in anearlier module using the repeat block. However, there are major differences between the forever/if combination of blocks and the repeat block. Some of those differences are illustrated in Image D , which shows a repeat block at the top and a forever block with an embedded if block near the center.
One of the major differences is the shape of the blocks that can be dropped into the pockets on the repeat block and the if block.
The blocks with the rounded ends in the upper half of Image D , which generally produce numeric results, cannot be droppedinto the pocket with the pointed ends on the if block. The if block requires a boolean value of true or false.
However, the blocks with the rounded ends can be dropped into the pocket with rounded ends on the repeat block, which requires a numeric value.
The blocks with the pointed ends in the lower half of Image D can be dropped into the pocket on the if block. Generally speaking, those blocks produce a boolean result of true or false.
Curiously, the blocks with the pointed ends can also be dropped into the pocket with the rounded ends on the repeat block. That pocket needs a numeric value to specify how many times its embedded code will beexecuted. My unconfirmed guess is that when one of the boolean blocks with the pointed ends is dropped into a repeat block, a false value is treated as 0 and a true value is treated as 1.
The repeat block doesn't require that any other block be dropped into the pocket to make it fully functional. All that is required isthat a literal value be entered into the pocket, or that the default literal value be accepted. Used in this way, the repeat block will cause the code inside the block to be executed a number of times matching the literal value.
On the other hand, the if block requires that another block, which evaluates to either true or false, bedropped into the pocket to make it functional.
In this program, I used a counter variable in conjunction with a less than block to produce a boolean result, thus causing the behavior of the forever/if block combination to be similar to the behavior of a repeat block. However, the forever block is much more versatile. The forever/if combination can be used to execute a seriesof code blocks on the basis of any block that you can construct that will evaluate to true or false.
Notification Switch
Would you like to follow the 'Teaching beginners to code' conversation and receive update notifications?