Recently, you wrote a program to have Guido go down 2nd Street and pick
up trash. You probably wrote code that used an if
statement
("if trash is here, pick it up") followed by a move
statement.
Then you copied and pasted that code until you had enough copies of it so
Guido would go all the way down 2nd Street, picking up all the trash.
There is a better way to do a group of statements over and over again:
the do
instruction. The do
instruction allows you to
repeat a set of actions a given number of times. For instance,
do 5: move
moves Guido 5 intersections forward. If you want to repeat multiple actions, group them together by indenting the instructions the same number of spaces:
do 5: putbeeper move
Using the same number of spaces to indent is mandatory if you want to repeat multiple actions. If you mistakenly write
do 5: putbeeper move
This code would put 5 beepers at one place and then move forward just one intersection. That's probably not what you wanted to happen. Be careful to keep indentation the same to keep groups of instructions together as one block.
Guido is smarter now and knows about the iterate statement. He is assigned once again to pick up trash along Second Street. Rewrite your solution to the previous assignment using Guido's new found power.
Copyright © 2003 Roger Frank.