LOGO Control Commands

LOGO Control Commands Programs to do Decision-making for Flow-control

LOGO Control Commands enable the program to make decisions depending on the test result of certain conditions. The Control commands are IF and IFELSE. The IF and IFELSE commands are used with the comparison operators.

Let’s first introduce Comparison Operators before we introduce the LOGO IF and IFELSE Control Commands,

Comparison Operators

In your daily life, you do comparisons every day such as who is taller, who is smaller, which of your friends is older than you, etc. The LOGO programs, similarly do these kinds of comparisons when they are run. These comparisons help the program to decide which branch of code to execute next.

LOGO has many Comparison Operators.  In this chapter, we will use just three comparison operators.

  • Greater than (symbol >)
  • Less than (symbol <)
  • Equal (Symbol =)

To explain these comparison operators, let’s assume that Michael_Age is 7, Steve_Age is 8, and Robert_Age is 7.  The table below explains the result of each comparison operation.

OPERATORDESCRIPTIONEXAMPLE
=Equal. If the values of on the two sides of the = operator are equal, then condition is true.Michael_Age = Steve_Age returns false.
Michael_Age = Robert_Age returns true.
Greater than. If the value on left of > operator is greater than value on right side of operator, it returns true, otherwise returns false.Michael_Age > Steve_Age returns false.
Michael_Age > Robert_Age returns false.
< Less than. If the value on left of < operator is less than value on right side of the operator, it returns true, otherwise returns false.Michael_Age < Steve_Age returns true.
Michael_Age < Robert_Age returns false.
LOGO COPARISON OPERATORS

We will use these comparison operators to see how these operators help us in writing programs using the LOGO Control Commands. The control commands are:

  • IFELSE
  • IF

Refer to this link for Control Commands information.

LOGO Control Commands (IF and IFELSE)

You make decisions every day in your life. For example, IF it is raining, you take a ride from your parents to go to school.  ELSE if it is sunny, you decide to ride your bike to school or walk to school. 

Similarly, computer programs have to make decisions when the program is running. The program makes these decisions by using the IF and IFELSE statements. Including this decision-making capability in the programs makes the programs intelligent. It gives the program the capability to make decisions at run-time depending on the result of some conditions. This is similar to when you decide to walk to school or get a ride based on the condition of rain or sunny weather.

To use the IF and IFELSE commands, you will specify certain conditions that the program will test for.  

For example, if your program has a test condition

IF 2=2

LOGO will evaluate it and determine it to be True

On the other hand, if your program has test condition

IF 2=3

LOGO will evaluate this test condition and determine it to be False.

As another example, if your program has test condition

IF 3 > 2

LOGO will evaluate the test result as True.

Based on the results of these tests, the program will decide which branch of code to execute next.

Control Commands with make Statement

In this section, we will see how to implement test conditions using variables. You will recall we defined variables using the make statement in the Arithmetic Commands chapter. 

In the code snippet below, we define two variables SOCCER_BALL and TENNIS_BALL, and assign them values.

The variable SOCCER_BALL represents the diameter of the soccer ball, which is 8.6 inches.  The variable TENNIS_BALL represents the diameter of the tennis ball, which is 2.7 inches.

make "SOCCER_BALL 8.6
make “TENNIS_BALL 2.7

Using these variables, we can write a control statement as follows:

IF :SOCCER_BALL > :TENNIS_BALL

The above test condition will evaluate to True

Note that we are using a colon (:) before the variable name because we are comparing values contained in these two variables.

In the following section, we will show how to use the IF commands to write programs using comparison operators.

IF Control Command

IF Statement syntax:
IF condition [Execute this list if condition evaluates to True]

Description: If condition evaluates to True, then the list within the brackets is executed.  If the condition evaluates to False, then do nothing.

The program below tests the condition if 1 = 1.  If it evaluates to True, the list within the brackets is executed.

In the first line of the program, the condition 1 = 1 evaluates to True. Therefore the program executes the list within the brackets and prints [CORRECT].

The second line of the program tests if 1 > 1. This condition evaluates to False. Since the condition evaluates to False, LOGO does not execute the list within the parenthesis and does nothing.

PROGRAM EXAMPLE: LOGO IF COMMAND
IF 1 = 1 [SHOW [CORRECT]]
[CORRECT]

IF 1 > 1 [SHOW [CORRECT]

IFELSE Control Command

LOGO provides another control command called IFELSE.

Syntax of IFELSE Control Command: 
IFELSE condition [Execute this list if condition evaluates to True] [Execute this list if condition evaluates to False]

Description: If condition evaluates to True, then the first list within the brackets is executed.  If the condition evaluates to False, then the second list within the brackets is executed.

In the program below, the first statement in the program tests the condition if 1 = 1. LOGO will evaluate this condition to be True, therefore the first list in the brackets is executed and prints CORRECT.

The second statement in the program below tests the condition if 1 = 5. LOGO will evaluate this condition to be False, therefore the second list in the brackets is executed and prints NOT CORRECT.

PROGRAM EXAMPLE: IFELSE COMMAND
IFELSE 1 = 1 [PRINT [CORRECT]] [PRINT [NOT CORRECT]]
CORRECT

IFELSE 1 = 5 [PRINT [CORRECT]] [PRINT [NOT CORRECT]]
NOT CORRECT

STOP Command

We discussed the REPEAT command in Loops in LOGO chapter. The REPEAT command tells the program how many times to repeat an operation. The REPEAT command is a bit inflexible in some programming situations. That is where the STOP command helps the programmer in removing this inflexibility.

What is The Use of Stop Command

The programmer often runs into situations, where the programmer would like to stop the REPEAT loop before it is completed. The REPEAT command does not give the programmer any control to stop the repeat operation depending on some test condition. 

This is where the STOP command comes in. To stop the REPEAT operation, the programmer will use the IF statement along with a comparison operator (>, <, =) to define the test condition. If the condition evaluates to True, the STOP command will stop the REPEAT operation.

The statement below shows how to use the IF and STOP commands to terminate a REPEAT loop. In this statement, we define a variable “SquareSide and then test using the comparison operation, followed by [STOP].

             IF :SquareSide < 200 [STOP]

If the value of the variable “SquareSide is less than 200, the STOP command is executed and the procedure stops. 

The next section shows in detail how we use the IF and STOP commands together to stop a REPEAT loop.

LOGO Graphics Programming Using Variables and STOP Command

Let us write a LOGO graphics program that uses a variable.

The program example below shows how you can use the IF and STOP commands to stop the REPEAT loop. We define the “SIZE variable as the length of the side of a triangle and set it to 120. At each iteration of the REPEAT loop, the “SIZE variable value is decremented by 5. When the “SIZE variable value reaches 50, the IF condition evaluates to True. As a result, the STOP command is executed, which stops further REPEAT iterations. In the absence of the IF-STOP, the size variable value will keep on decrementing to zero and continue decrementing to negative values. The REPEAT will never stop.

Run this program on your computer and play with different values of the SIZE variable. Then check at which variable value, the IF and STOP commands stop further repeat loops.

PROGRAM EXAMPLE: TURTLE GRAPHICS USING MAKE AND STOP COMMANDS
; Define a procedure called TRIANGLE
TO TRIANGLE ; Step 1)
REPEAT 3 [FORWARD :SIZE RIGHT 120]; Turtle moves forward by a values contained in the variable SIZE.
End
; End of procedure TRIANGLE

; Define a procedure called MANY_TRIANGLES
TO MANY_TRIANGLES; Step 2)
REPEAT 36[
TRIANGLE; Step 3) Call ‘TRIANGLE’ procedure to draw a triangle.
RIGHT 10; Step 4) Turn the turtle right by 10 degrees.
MAKE "SIZE :SIZE – 5; Step 5) Reduce the value of SIZE variable by 5. 
IF :SIZE < 50 [STOP]; Step 6) when the variable SIZE is less than 50, stop the REPEAT procedure;
]
End ; End of procedure MANY_TRIANGLES

; Start of Main program
SETPENCOLOR 4; Set pen color to red.
MAKE “SIZE 120; Step 7)
MANY_TRIANGLES; Step 8) Call procedure MANY_TRIANGLES; Step 6)

Detailed Step-by-step Description of the program:

Step 1) TO TRIANGLE

Define a procedure named TRIANGLE using the TO statement. The length of the triangle side is a parameter :SIZE.

Step 2) TO MANY_TRIANGLES  

Define a procedure called MANY_TRIANGLES. 

The body of MANY_YRIANGLES has the following code.

Step 3): The MANY_TRIANGLES procedure has a loop that repeats 36 times. This procedure calls the procedure TRIANGLE defined in step 1). This code will repeat 36 times unless terminated by the IF-STOP command earlier (See Step 6)

Step 4) When the TRIANGLE is drawn, this step turns the TRIANGLE RIGHT by 10 degrees to get ready for the next iteration of the loop.

Step 5) MAKE “SIZE :SIZE – 5

In Step 5), the variable “SIZE value is decreased by 5 at each REPEAT loop iteration.  Therefore, the TRIANGLE size drawn in the next iteration is 5 pixels smaller.

As a result of Step 5), the value of :SIZE will keep on decrementing by 5 pixels at each REPEAT iteration.

Step 6) IF :SIZE < 50 [STOP]

In Step 6), the program performs a condition test (using the IF command) to check if the :SIZE value is less than 50. If value of :SIZE is less than 50, the STOP command is executed and the REPEAT iterations stop, instead of continuing forever.

Main Program code starts here

Step 7) MAKE “SIZE 120

In the main program, step 7) initializes the TRIANGLE size to 120 at the start of the main program by assigning a value of 120 to “SIZE variable.

Step 8) MANY_TRIANGLES

In Step 8), the main program calls the procedure MANY_TRIANGLE.

The following is the program’s output. Notice that the program does not REPEAT through 36 loops. The STOP command ends the REPEAT loop when the SIZE variable reaches 50.  It draws 20 triangles (150 – 50)/5 = 20 and then exits from the REPEAT loop.

Control Commands using IF and STOP commands to break out of the REPEAT loop.
USE IF AND STOP COMMANDS TO STOP A REPEAT LOOP

Copyright © 2019 – 2023 softwareprogramming4kids.com

All Rights Reserved

Verified by MonsterInsights