Loops in LOGO

Loops – A Means To Repeat A Section of LOGO Code

On this page, we will see how LOGO provides us with means to use loops to repeat a set of statements using REPEAT and REPCOUNT commands.

So far, we have learned how to draw polygons. This is done by repeating the same command a number of times. For example, in the case of an octagon, we need to write the command FORWARD 90 Right 45 eight times. That is a bit tedious and not too exciting to write the same line of code several times. Surely, there must be a better way to achieve this goal. Indeed, there is.

The LOGO language has a few commands that allow the programmer an easier way to repeat a block of code.  These commands are:

  • REPEAT Statement
  • FOR statement
  • WHILE Statement

We will start learning about loops with the REPEAT Statement.

For more information, go to this link.

REPEAT Command for Loops

Let us review how we drew the square in the previous chapter. We wrote the following commands to draw a square with sides = 300. We typed the command FORWARD 300 RIGHT 90.

Since the square has four sides and the turn angle is 90 degrees, we repeated the command FORWARD 300 RIGHT 90 four times.

Our program to draw a square looked like the following. These four statements make the turtle draw a square of side 300 pixels.

FORWARD 300 RIGHT 90
FORWARD 300 RIGHT 90
FORWARD 300 RIGHT 90
FORWARD 300 RIGHT 90

The LOGO REPEAT command can be used to draw a shape such as a polygon by typing just one command instead of having to type the same commands several times using loops.

How does REPEAT command work?

If you want to draw a square using the REPEAT command, you give just the command FORWARD 300 RIGHT 90 enclosed in square brackets and type REPEAT 4 in front of the opening square bracket as below.

Repeat 4[Forward 300 right 90]

The number ‘4’ in the statement will repeat the FORWARD 300 RIGHT 90 statement four times and draw a square. The REPEAT instruction repeats the instruction within the square brackets […] as many times as the number after the REPEAT word and before the square opening bracket.

The following is the syntax of the REPEAT Statement.

REPEAT count instructionlist

count is the number of times the instructionlist is to be repeated.

instructionlist is the code to be repeated.

The program below uses the REPEAT command to draw a square with sides of 300 pixels.

REPEAT 4[FORWARD 300 RIGHT 90]

Below is what the turtle draws when you run the command

Loops in LOGO. Draw a square using REPEAT Command.
SQUARE Using REPEAT COMMAND. REPEAT 4 [FORWARD 300 RIGHT 90]

Problem Solving

  1. Use loops to draw a pentagon using REPEAT command
  2. Draw a hexagon using REPEAT command
  3. Same as above. Draw a octagon using REPEAT command
  4. Draw a decagon using REPEAT command

Loops To Draw a Circle With REPEAT Command

Now let us teach the Turtle how to draw a circle. The circle does not have any sides and does not have any angles like all polygons do. So, we will move the turtle forward by a small distance ‘of 2’ pixels and turn the turtle by a small ‘1’ degree angle.  We will repeat (loop) the command 1,000 times since the circle can be thought to have an infinite number of ‘sides’.

REPEAT 1000 [FORWARD 2 RIGHT 1]

See the result below of what the above command draws.

Loops in LOGO Draw a Circle Using REPEAT Command.
Circle Using REPEAT Command: REPEAT 1000 [FORWARD 3 RIGHT 1]

Enrichment

 What do you think will be the result if to draw a circle, we move the turtle forward by 5 and change the angle to 5 degrees at each iteration of REPEAT instruction? Do you think the circle shape will change?  Will it still look like a smooth circle, or will it show a circle that is not very smooth?  Why?  Will the circle be bigger when you use ‘FD 5’ instead of ‘FD 1’

Loops using the FOR Command

LOGO programming language has another way to loop (repeat) a list of instructions.

Syntax: FOR [control list]  [instruction to be executed]

The FOR loop command starts with a keyword FOR.

The second part of the instruction is the Control list input that controls how the iteration occurs.

The third part of the FOR command is the instructions to be executed according to what the Control list specifies.

Note: The FOR loop uses the concept of variables, which we have not introduced so far. For LOGO variables introduction, refer to page Arithmetic Commands. You should review the LOGO Arithmetic Commands page first before attempting to read this section.

The example below explains the FOR loop command.

PROGRAM EXAMPLE: for LOOP STATEMENT
FOR [x 1 10 2] [PRINT :x]   ;1)
1
3
5
7
9

Detailed Explanation of the Program:

1) FOR [x 1 10 2] [PRINT :x] 

[x 1 10 2] is the control list, which specifies how to execute the [PRINT :x] instructions.

In the control list [x 1 10 2], x is a variable.  The first number 1 is the start value of the variable x, the second number 10 is the stop value of the variable x. The third number 2 is the step value by which the variable x is incremented in each iteration of the loop. The variable x steps from 1 to 10 with increments of 2, that is x takes the value 1, 3, 5, 7, and 9.  In the first loop, it sets x = 1. It executes the instruction [PRINT :x]. Since x = 1 in this iteration of the loop, it prints the value of x, which 1. 

In the second iteration of the FOR loop, x is incremented by the step value ‘2’, so x becomes 3. It again executes the [PRINT :x] instruction. Since x = 3 in this iteration, the program prints 3.  

In the next loop, x is incremented by the step value ‘2’. Therefore, x becomes 5, which is printed by the [PRINT :x] statement, and the program prints 5.  This continues until x reaches the stop value of 10.

FOR Repeat Command To Draw A Spiral

Let us try to make some interesting shapes using the FOR loop command.  The program example below uses the same instruction as the above example but replaces the [PRINT :x] part of the instruction with [FORWARD 10 + :x RIGHT 20].

PROGRAM EXAMPLE: for LOOP – DRAW A SPIRAL
CLEARSCREEN
FOR [x 1 35 1] [FORWARD 10 + :x RIGHT 20;   Step 1)

The following is the shape that the turtle drew on running the program.

Loops in LOGO. Use FOR Loop to Draw a Spiral.
The FOR Loop – Draw a Spiral

Detailed Explanation of the program:

Step 1): FOR [x 1 35 1] [FORWARD 10 + :x RIGHT 20]

This statement loops 35 times and executes the instruction [FORWARD 10 + :x RIGHT 20] a total of 35 times.

In the first loop, the variable x value is 1. So, the turtle moves forward by 10 + 1 pixels and then turns right 20 degrees.

Then, the program starts the second loop. In this iteration, the x variable is incremented by the step value, which is 1. Therefore, x = 2 in this iteration. The command FORWARD 10 + :x is executed. Therefore, the turtle moves 10 + 2 = 12 pixels and then turns right by 20 degrees.

For the third loop, x is incremented by the step value, so x = 3. Therefore, the command FORWARD 10 + :x results in the turtle moving by 10 + 3 = 13 pixels and the turns right 20 degrees. 

This continues until the 35th loop.  For this loop, x = 35.  So, the turtle moves forward 10 + 35 = 45 pixels and turns right 20 degrees.

The result is a spiral as shown in the screenshot. Note the colon symbol (:) in front of the variable x.  We will explain this in detail in the Variables section of page LOGO Arithmetic Commands chapter.

Loops Using The WHILE Command

LOGO programming language has another way to loop through a set of statements.  It is called the WHILE loop. The difference between the FOR loop and WHILE loop is that whereas the FOR loop steps through the [control list] and executes the [instruction list], the WHILE loop steps through and executes the instructionlist as long as the conditionlist evaluates to True.  LOGO continues to evaluate the conditionlist after each iteration of the loop.  If the conditionlist evaluates to False, the looping is terminated.

The following is the syntax of the WHILE instruction.

WHILE conditionlist instructionlist

Note: The WHILE loop uses the concept of variables and make command, which we have not introduced so far. For LOGO variables and make command introduction, refer to page LOGO Arithmetic Commands. You may want to review the LOGO Arithmetic Commands chapter first before attempting to read this section.

Let us explain the WHILE loop by using an example. The program below keeps on printing numbers from 2 until conditionlist [x < 10} is True.

PROGRAM EXAMPLE: THE WHILE LOOP
make "x 1 ; Step 1)
while [:x < 10] [make "x :x+1 print :x] ;Step 2)
2
3
4
5
6
7
8
9
10

Detailed Description of the Program:

Step 1) make "x 1

This step defines a variable x and assigns it the value of 1.  Note the colon (“) in front of the variable x.

Step 2) while [:x < 10] [make "x :x+1 print :x]

The WHILE statement first evaluates [:x < 10] condition.  If the condition evaluates to True, it executes the statement [make "x :x+1 print :x]. In this statement, it prints the value contained in variable x, then increments the value of x by 1.

At this stage, x becomes 2. Then, the WHILE loop goes back and tests the condition [:x < 10].  If x is still less than 10, it executes the statement [make "x :x+1 print :x] that prints the new value of x, which is 2 now, and then increment x by 1 to 3.  This looping process continues until x becomes 10.

Use WHILE Command To Draw A Spiral

Now that we know how the WHILE loop works, let us write a program to create the same spiral that we created using the FOR loop, except in this case we will make the spiral color red.

PROGRAM EXAMPLE: THE WHILE LOOP – DRAW A SPIRAL
CLEARSCREEN
MAKE "x 1
SETPENCOLOR "RED
WHILE [:x < 35] [MAKE "x :x+1 FORWARD 10 + :x RIGHT 20];1)

Detailed Explanation of the Program:

Step 1) WHILE [:x < 35] [MAKE "x :x+1 FORWARD 10 + :x RIGHT 20]

The operation of the WHILE loop is very similar to the FOR loop program. At every iteration of the loop, the variable x is incremented by 1.  On the first loop, x = 1.  So, the turtle moves forward by 10 + 1 = 11 pixels.

On the second loop, x = 2.  So, the turtle moves forward by 10 + 2 = 12 pixels, and so on. 

The only difference between the FOR and WHILE loops is that whereas the FOR loop steps through the [control list] at each iteration of the loop, the WHILE loop evaluates the conditionlist to see if it is True or False, before starting the next loop.

In this program example, the conditionlist evaluates if the variable x is less than 35.  Once the variable x reaches 35, the looping stops.

The following is the shape that the turtle drew when we run this program.  Run this program on your computer with different values of FORWARD commands and ANGLE values to see what kind of spirals are drawn by the turtle.

Loops in LOGO. Draw a Spiral Using the WHILE Loop Using Red Pen.
The WHILE Loop to Draw a Spiral. Use Red pen.

REPCOUNT Command

You learned about the REPEAT command in the previous section.  There is a more powerful command that can make interesting, complex figures. It is called REPCOUNT command.

REPCOUNT command works similarly to the REPEAT command you used in drawing SQUARE or PENTAGON, but it changes the size and, or the angle, or both a little bit at each iteration of the REPEAT instruction. Since the size or the angle changes at each iteration of the REPEAT command, the turtle will draw complex and interesting shapes such as flowers, spirals, etc.

REPCOUNT Command to Draw a Square Spiral

The following program draws a square, but at each turn of the REPEAT command, the turtle will draw squares with sides that are four times as large as compared to the previous repeat iteration. The resulting shape is a spiral made of squares.

REPEAT 100 [FORWARD REPCOUNT*4 RIGHT 90]

In the above program, we loop the code within the square brackets 100 times. Now, let us examine what is inside the square brackets. In the previous SQUARE program, we typed FORWARD 300 RIGHT 90 inside the brackets. In the above instruction, we replace “FORWARD 300 RIGHT 90” with “FORWARD REPCOUNT*4 RIGHT 90“.

So how does REPCOUNT command work?

REPCOUNT starts at a value of 1 in the first REPEAT loop. On each subsequent REPEAT loop, REPCOUNT is incremented by 1.

For the first REPEAT iteration, REPCOUNT = 1, so the turtle will move forward 1*4 = 4 pixels, so it will draw a square with sides = 4. 

On the second REPEAT iteration, REPCOUNT = 2, so the turtle moves by 2*4 = 8 pixels, so the turtle will draw a square with sides = 8, and so on. 

On the 100th REPEAT turn, REPCOUNT = 100, so the turtle will move 100*4 = 400 pixels and will draw a square with sides = 400.  Here is the result.

Loops in LOGO. Drawe a square spiral using REPEAT and REPCOUNT commands
Square Spiral Using REPCOUNT Command REPEAT 100 [FD REPCOUNT *4 RT 90]

Enrichment

1. Try the following command and see what will the turtle draw. REPEAT 100 [ FORWARD REPCOUNT + 10 Right 90 ] What does the spiral look like? Is the spiral bigger or smaller than in the above example? Why?

   2. Try the following command and see what the turtle will draw. REPEAT 100 [ FD REPCOUNT +10  RIGHT 120 ] What kind of spiral do you think the turtle will draw?  Is it made of triangles? pentagons? Some other polygon? Why?

   3. If you change the angle from 120 to 72 in the above statement, the turtle will draw a spiral of which polygon?

   4. Play with different angles like 45, 60, 72, 90, and 120 and see what kind of spirals are drawn by the Turtle.

   5. Change the ‘+10’ in the above statement to multiply by number *5 and see what the turtle draws.

REPEAT and REPCOUNT Commands Together

We learned about the REPEAT command in Loops in LOGO page. Now we will use the REPEAT command along with the REPCOUNT command that will enable us to make the turtle draw more interesting shapes like a spiral, star, and many more by using both REPEAT and REPCOUNT commands together in the same program.

Draw a Star

This program makes use of the TO statement we learned on the previous page to add the word ‘STAR’ to the turtle’s vocabulary.  The program will REPEAT 45 times what is inside the brackets [FORWARD REPCOUNT * 5 RIGHT 144].

In the first REPEAT turn, REPCOUNT = 1, so the turtle will move forward by 1 x 5 = 5 pixels and then turn right by 144 degrees.

In the second REPEAT turn, REPCOUNT = 2, so the turtle will move forward by 2 x 5 = 10 pixels and then turn right by 144 degrees.

On the 45th REPEAT turn, REPCOUNT = 45, so the turtle will move by 45 x 5 = 225 pixels and then turn right by 144 degrees.


Below are the program and the computer-produced image.

PROGRAM EXAMPLE: DRAW A STAR USING REPEAT AND REPCOUNT COMMANDS
TO STAR  ;Add ‘STAR’ to turtle’s vocabulary
  REPEAT 45 [ FORWARD REPCOUNT * 5  RIGHT 144 ]
END

STAR  ;Call Procedure ‘STAR’
Loops in LOGO. Draw a star Using REPEAT and REPCOUNT Commands.
DRAW A STAR USING REPEAT AND REPCOUNT COMMANDS. REPEAT 100 [FD REPCOUNT *4 RT 90]

Enrichment

  1. Change multiplier ‘5’ to a different number and see the change of shape.
  2. Change the angle ‘144’ to a different angle like 143, or 140, and see what happens.

Draw Spiral Using REPEAT and REPCOUNT Command

The following program uses REPEAT and REPCOUNT instructions to draw a Spiral.  REPEAT instruction will execute 100 times the instructions that are inside the square brackets […….].

PROGRAM EXAMPLE: DRAW A SPIRAL USING REPEAT AND REPCOUNT COMMANDS
TO SPIRAL ; Add ‘SPIRAL’ to the turtle’s vocabulary
REPEAT 100 [ FORWARD REPCOUNT * 4  RIGHT 91 ]
END

SPIRAL; Call procedure "SPIRAL"
Loops in LOGO. Draw a spiral using REPEAT and REPCOUNT Commands.
Draw A SPIRAL USING REPEAT and REPCOUNT Commands REPEAT 100 [FORWARD REPCOUNT * 4 RIGHT 91]

Enrichment

  1. Try smaller number of REPEAT instead of 100 and see what happens.  Explain why.
  2. Try REPCOUNT multiplier of *4 instead of *2 and see what happens.  Explain the difference in shape.
  3. TRY an angle of 89, or 92 instead of 91, and see what happens.  Explain the difference.

Copyright © 2019 – 2021 softwareprogramming4kids.com

All Rights Reserved

Verified by MonsterInsights