6. The Garden with Repetitions <<< Contents >>> 8. Peter Walks on Marks


7. The Garden with a Condition

The item in front of Peter element can be used not only for laying an item in front of Peter, but also for detecting what item lies in front of Peter. We will use such testing in another method of planting the garden, based on a cycle with a condition.

In the Basic Elements, Trash window, find the conditional repeating of commands element in the program control group. Drag it to the beginning of the program from the last chapter (in front of all commands). Move the commands for making a step and laying down a flower in front of Peter into the repeat commands cycle body. Leave the commands for turning left and waiting for a key to be pressed after the cycle. You can discard the rest of the commands.

The while is valid cycle element tests a condition, which specifies how long should the commands in the cycle be repeated. Into the condition, we will put a test detecting whether there is an empty square in front of Peter. To assemble the condition, we will use the item identity element. It is in the Basic Elements, Trash window, in the sheet group. Drag this element into the while is valid cycle element. Into the item identity element, we will insert two elements that will be compared. The first one is item in front of Peter ; the second is empty square (from the Global Variables and Functions window). Here is the result:

Try to run the program. Peter plants flowers to the right border of the window, and then he turns up and waits for a key to be pressed. If the function of the program is not clear to you, we can take a closer look at it.

How does conditional repeating work? The cycle elements descriptions tell us: "while is valid (something), repeat commands (something)". At a closer look, it means the following: In the beginning, the cycle detects, whether the condition is true. If it is, the commands in the cycle body are performed. Everything is repeated from the beginning then. The condition is evaluated again, and if it is true, the commands are performed again. If the condition is not valid, nothing is performed, the cycle ends, and the program continues by performing the commands after the cycle.

The program could be described in this way: In the beginning, the cycle asks the testing function that evaluates the condition: "Is the condition true?" The testing function here is the function for comparing items. It detects: "Is there an empty square in front of Peter?" If there is, it replies to the cycle: "Yes, the condition is true." In that case, the cycle performs the commands in its body — Peter lays a flower and makes a step. This is repeated until Peter reaches the border of the sheet. The testing function detects that there is not an empty square in front of Peter now, and tells so to the cycle. The cycle does not continue. After that, Peter turns left, and the program pauses and waits for a key to be pressed.

After laying flowers in one row, Peter stays turned left, heading another row. We will test, whether there is an empty square in front of him, and if there is, we will tell him to plant another row. When he gets back to his original position, there will not be an empty square in front of him, but a flower that he has planted, and so he will stop.

This means that now we will take another conditional repeating of commands cycle. We will put it to the beginning of the program, and into its body, we will move (using the left mouse button) the previously created cycle and the command for turning left. The command for waiting for a key to be pressed stays at the end of the program. Into the outer cycle condition, copy (using the right mouse button) the condition testing an empty square in front of Peter (you do this by dragging the item identity element). Here is the result:

The program is ready; all that remains is to test it.


6. The Garden with Repetitions <<< Contents >>> 8. Peter Walks on Marks