Spider | Previous | Next | Full Table of Contents | Index | Program List | Copyright
Now that you know how to write loops, let's return to the spider.
Recall Program 3.13, in which we
asked the spider to draw a box using a large number of Step
commands. This is much more easily done using a loop.
Program 5.10 shows how the box can be drawn
using a single loop with three Step
commands in the
loop body. The loop counter runs from 1 to 4 because the body is
repeated for each of the four sides. The basic algorithm is
1. FOR
each of four sides LOOP
2.Draw one side of the box
END LOOP;
Step 2 is refined to
2.1 Step
2.2 Step
2.3 Step
2.4 Turn
We omit the sample run because the output is very similar to the original from Program 3.13.
Program 5.10
Drawing a Box with a Loop
WITH Spider; PROCEDURE Draw_Box_with_1_Loop IS ------------------------------------------------------------------------ --| Draw 4 x 4 box with spider - use loop --| Author: Michael B. Feldman, The George Washington University --| Last Modified: July 1995 ------------------------------------------------------------------------ BEGIN -- Draw_Box_with_1_Loop Spider.Start; FOR Side IN 1..4 LOOP Spider.Step; Spider.Step; Spider.Step; Spider.Right; END LOOP; Spider.Quit; END Draw_Box_with_1_Loop;
A different refinement of step 2 takes note of the repetitious
nature of the Step
commands:
2.1 FOR
each of three steps LOOP
2.2 Step
END LOOP;
2.3 Turn
so that the overall algorithm is now:
1. FOR
each of four sides LOOP
2. Draw one side of the box
2.1 FOR
each of three
steps LOOP
2.2 Step
END LOOP;
2.3 Turn
END LOOP;
and this algorithm is implemented in Program 5.11. Note that we could very easily change the number of steps per side just by changing the upper bound of the inner loop.
Program 5.11
Drawing a Box with Nested Loops
WITH Spider; PROCEDURE Draw_Box_with_Loops IS ------------------------------------------------------------------------ --| Draw 4 x 4 box with spider - use nested loops --| Author: Michael B. Feldman, The George Washington University --| Last Modified: July 1995 ------------------------------------------------------------------------ BEGIN -- Draw_Box_with_Loops Spider.Start; FOR Side IN 1..4 LOOP FOR Count IN 1..5 LOOP Spider.Step; END LOOP; Spider.Right; END LOOP; Spider.Quit; END Draw_Box_with_Loops;
Finally, consider what happens if we make the upper bound of the inner loop different for each iteration of the outer loop. Specifically, suppose we set this upper bound to be the current value of the outer counter. The resulting shape would be a spiral. This is shown in Program 5.12, together with some sample output that shows a 10-segment spiral pattern. Be sure you trace this program carefully to understand how and why the spiral is drawn.
Program 5.12
Drawing a Spiral
WITH Spider; PROCEDURE Spiral IS ------------------------------------------------------------------------ --| Draw spiral pattern with spider - use nested loops --| Author: Michael B. Feldman, The George Washington University --| Last Modified: July 1995 ------------------------------------------------------------------------ BEGIN -- Spiral Spider.Start; Spider.Blue; FOR Line IN 1..10 LOOP FOR Count IN 1..Line LOOP Spider.Step; END LOOP; Spider.Right; END LOOP; Spider.Quit; END Spiral;
Sample Run
----------------------------------------- --- |. . . . . . . . . . . . . . . . . . . . .| | V | |. . . . . . . . . . . . . . . . . . . . .| | X | |. . . . . . . . . . . . . . . . . . . . .| --- |. . . . . . . . . . . . . . . . . . . . .| |. . . . . . . . . . . . . . . . . . . . .| |. . . . . . X X X X X X X X X X * . . . .| |. . . . . . X . . . . . . . . . . . . . .| |. . . . . . X . X X X X X X X . . . . . .| |. . . . . . X . X . . . . . X . . . . . .| |. . . . . . X . X . X X X . X . . . . . .| |. . . . . . X . X . X . X . X . . . . . .| |. . . . . . X . X . . . X . X . . . . . .| |. . . . . . X . X X X X X . X . . . . . .| |. . . . . . X . . . . . . . X . . . . . .| |. . . . . . X X X X X X X X X . . . . . .| |. . . . . . . . . . . . . . . . . . . . .| |. . . . . . . . . . . . . . . . . . . . .| |. . . . . . . . . . . . . . . . . . . . .| |. . . . . . . . . . . . . . . . . . . . .| |. . . . . . . . . . . . . . . . . . . . .| |. . . . . . . . . . . . . . . . . . . . .| -----------------------------------------
Copyright ©1996 by Addison-Wesley Publishing Company, Inc.