5. Peter's Garden <<< Contents >>> 7. The Garden with a Condition
Imagine Peter wanting to plant a garden
that would be 15 squares wide and high. We could make more copies
of the commands from the previous chapter, but this would make
any programmer feel ashamed .
For this reason, we will learn how to use a
cycle. A cycle ensures repeated execution of commands. We
will use a cycle with a set number of repetitions. It is labeled command repeating with
specified run number , and you can find it
in the Basic
Elements, Trash window, in the
program control
group. Drag it to the very beginning of the program from the
previous chapter (in front of all of the commands).
When you drop the cycle element, notice
that two more elements came with it. These two elements are a
natural part of the cycle, and cannot be deleted, nor moved
somewhere else. The first element is labeled for number of repetitions ,
and it specifies the number of times that the cycle commands will
be executed. The second element is called repeat commands
.
Here you put the cycle commands that you want to repeat. It is
called a cycle
body.
In the beginning, we specify the commands
that will be executed in the cycle. From the previous chapter, we
have a step command, and a command that lays a flower in front of
Peter. Drag these two commands into the cycle body, that is, into
the repeat
commands element. Just to
remind you the command for laying the flower in front of
Peter had been created from two elements, the item in front of Peter
element and the connected element called Flower
.
Now we specify the number of repetitions,
for which the commands in the cycle will be executed. For this,
we need the numeric
constant element from the calculations
group. Drag this element into the for number of repetitions
element. If you expand the numeric constant element in the Basic Elements window, you will find elements for the 0
to 9
digits in it. Drag the digits into the numeric constant in the
program to create a number of repetitions. The number that you
create is read from the top. As the number of repetition, we will
choose a number that is smaller by one than the garden width.
That is, we will set the number to 14
the constant will contain the digit 1 and the
digit 4 under it.
There is also another way of specifying numbers. If the numeric constant does not contain any digit element, the number in the descriptive text of the numeric constant is used. There may be any notes behind the number. In our example, we could only type the text 14 number of steps as the elements description. Anyway, the previous way is more illustrative.
Now you can throw all of the remaining
elements outside the cycle into trash (the upper right window),
with the exception of one command for turning left, and the
command that waits for a key to be pressed. You can move elements
into trash with your mouse, but you can do it more quickly with
your keyboard. Click to select the icon of the first element you
want to delete, and then press the Delete
key, until all of the unnecessary elements are deleted. Be
careful not to press the key for too many times, otherwise you
would have to use the Undo button.
After the modifications, the program should look like this:
Notice that thanks to the elements descriptions, the program can be read in quite a natural way: "For number of repetitions 14, repeat commands: lay down a Flower item in front of Peter, then make a step."
Run the program. Peter will go towards the right border, laying down flowers on his way. When he plants them along almost the entire bottom border of the window, he turns up and waits. He forgot to plant a flower at the beginning of the line (on the left), but his next attempt will surely be better.
Now we could copy the cycle for three times, as we need four sides. However, being good programmers, we will use a cycle again. Cycle is, in fact, a normal command, and so we can nest it inside another cycle.
From the Basic Elements window, we will drag another command repeating with
specified run number element, and insert it
in the very beginning of the program. As a number of repetitions,
we will specify the number 4. For
numeric constants with one digit, we can use the digits alone. We
will find them by the numeric constant element. This means that
we will drag the 4
digit element into the for
number of repetitions
cycle parameter.
Into the repeat commands
cycle body, we will drag the cycle for planting one side of the
garden, which we have created before, and under it, a command for
turning to the left. The command for waiting for a key to be
pressed will be left in the very end of the program. A good
programmer also adds comments to his work, so that the function
of the program is clear to him or her even after a time; and not
only to him or her, but also to everybody else who ever sees the
program.
Here is the result of our hard work:
Run the program. Peter will go around his new garden, planting flowers on his way. This time he plants them all, and does not forget anything. Do you like his garden?
If you increase the number of steps in the inner cycle to e.g. 44, you will see that Peter plants the flowers around really the whole of the garden now, and that he does not even mind that in the end of his way, he always hits his head against the wall. If he cannot perform an operation, he simply does not perform it, and nothing happens.
If you want to do some more experimenting,
try to increase the number of repetitions in the main (outer)
cycle. Peter will keep on walking around the border of the
window. If the number of repetitions is too high, you might not
want to wait until the end of the program. In such a case, quit
the program by clicking the button (in the upper right corner of the window), or
by pressing Alt+F4.
5. Peter's Garden <<< Contents >>> 7. The Garden with a Condition