<< Chapter < Page | Chapter >> Page > |
The first thing that happens following entry is that a condition is tested for true or false.
If the test returns false, control simply exits the structure without taking any action at all.
If the test returns true:
During each iteration , if the test returns false, control exits the structure. If the test returns true, the entire process is repeated.
Each of the action elements may be implemented by another sequence, selection, or loop structure.
Eventually all of the actions will be completed and the condition will be tested again.
Generally speaking, unless something is done in one of the actions to cause the test to eventually return false, control will never exit the loop.
In this case, the program will be caught in what is commonly called an infinite loop.
In some programming languages, there are structures other than sequence, selection, and loop that structured-programming experts are willing toaccept for convenience including:
While sometimes more convenient than the three main structures, these structures are not required for the solution of programming logic problems.
In this module, I will present and explain the simplest example of a selection structure that I was able to write in Scratch without using variablesand without using relational or logical operators. (I will explain operators, including relational and logical operators in future modules.)
The program places a basketball and two beachballs on the Stage as shown in Image 4 .
When the user clicks the green flag in the upper right corner of the stage, the three balls are placed in a horizontal line with the basketball in the center.
Scratch code, which can be paraphrased as shown in Image 5 , is executed each time the user clicks the basketball with the mouse.
In other words, if you repetitively click the basketball with the mouse, it will move back and forth from left to right bouncing off of the two beachballs.The basketball will keep bouncing back and forth between the two beachballs for as long as you continue clicking on the basketball.
Let's walk through the steps required to develop this program. I will deal first with the code that defines the behavior of the program when the userclicks the green flag in the upper right corner of the stage in Image 4 .
Notification Switch
Would you like to follow the 'Teaching beginners to code' conversation and receive update notifications?