How Can We Solve Arithmetic Problems Using LOGO Arithmetic Commands
The LOGO Arithmetic Commands enable us to solve arithmetic problems. These commands are SUM, DIFFERENCE, PRODUCT, and QUOTIENT. Variables and make command are introduced.
Let’s introduce the concept of Variables before we describe LOGO Arithmetic commands.
LOGO Variables
A variable is the name of the location in the computer memory that contains a value. Each memory location in the computer has an address. The computer’s CPU uses this address to read the value stored in the memory location. The computer’s CPU also uses this address to write a newly computed value to the memory location. The variables have a name consisting of strings (text). The CPU uses this string name to access the memory location to read the value stored in the memory location.
Strings are discussed in detail in the Data Types chapter in the Python section of this website. Variables are discussed in more detail in the Python section of this website in the Variables chapter.
How do we declare a variable in LOGO?
Declaration of a variable in LOGO is done by adding a quote symbol (“) in front of the variable string.
Let’s say your program would like to use the string size as the name of a variable. If the LOGO interpreter sees a quote symbol preceding the name size as in “size, LOGO will treat the string size as a variable.
How Do We Assign a Value to a LOGO Variable?
The value that the variable contains is assigned using the make
command, as in the statement below.
make "size 200
In this statement, size is a variable. The make
command assigns the variable size the value 200.
Note: If you put a quote symbol (“) in front of a string, LOGO treats it as a variable. On the other hand, if you put a colon symbol (:) in front of the string, LOGO treats it as the value stored in that variable.
Thus, as an example, “size denotes a variable named size.
and :size denotes the value contained in the variable size.
Since :size is a value, the statement FORWARD :size
will move the turtle forward by a number contained in the variable “size.
To reiterate, “size refers to the variable named size, and :size refers to the value contained in the variable size.
Having cleared the understanding of variables and the make command, let’s check out the LOGO Arithmetic commands.
More information on LOGO Variables and make command is at this link.
Arithmetic Commands Description
LOGO provides many Arithmetic Commands to do addition (+), subtraction (-), multiplication (*), and division (/). The names of these commands are:
- SUM
- DIFFERENCE
- PRODUCT
- QUOTIENT
These arithmetic commands can be used in LOGO programs to solve arithmetic problems. The table below explains the syntax of these Arithmetic Commands and how these commands work.
COMMAND | DESCRIPTION | EXAMPLE |
---|---|---|
SUM addend1 addend2 (SUM addend1 addend2 …) addend1 + addend2 | Outputs the sum of its inputs. If no inputs are given, then SUM outputs 0. | SHOW 2 + 3 5 SHOW (SUM 4 5) 9 SHOW (SUM 4 5 6) 15 |
DIFFERENCE number1 number2 number1 – number2 | Outputs the difference of its inputs. | SHOW 9 – 5 4 SHOW DIFFERENCE 7 4 3 SHOW (DIFFERENCE 9 5) 4 PRINT DIFFERENCE 7 4 3 |
PRODUCT factor1 factor2 (PRODUCT factor1 factor2 …) factor1 * factor2 | Outputs the product of its inputs (factor 1 * factor2). If no inputs are given, then PRODUCT outputs 1. | SHOW 4 * 6 24 SHOW (PRODUCT 4 6) 24 SHOW (PRODUCT 3 4 5) 60 |
QUOTIENT dividend divisor (QUOTIENT divisor) dividend / divisor | Divides first number (dividend) by the second number (divisor) and outputs the quotient of its inputs, that is it outputs dividend / divisor. When the single input is given, then QUOTIENT outputs the reciprocal of the input (as in example QUOTIENT 4), the result is ¼ = 0.25 It is an error if the divisor is zero. | SHOW 8 / 4 2 SHOW 8 / 3 2.66666666666667 SHOW (QUOTIENT 7 3) 2.3333333333333 SHOW (QUOTIENT 4) 0.25 |
Arithmetic Operations Using Variables and make command
The program below illustrates how the arithmetic commands combined with variables and the make
command are implemented in a program.
To view the result of the program, we use the OUTPUT commands SHOW and PRINT that we discussed on the LOGO Output Commands page.
PROGRAM EXAMPLE: LOGO ARITHMETIC OPERATIONS
; LOGO ARITHMETIC OPERATIONS USING VARIABLES
; AND Output results using SHOW AND PRINT COMMANDS.
; Define two variables “NUMBER1” and “NUMBER2”.
MAKE "NUMBER1 37
MAKE "NUMBER2 23
; Addition operation. Output result using SHOW command
SHOW :NUMBER1 + :NUMBER2
60
; Addition operation using SUM command
SHOW (SUM :NUMBER1 :NUMBER2)
60
;
; Subtraction operation. Output result using SHOW command
SHOW :NUMBER1 - :NUMBER2
14
; Subtraction using DIFFERENCE command
SHOW (DIFFERENCE :NUMBER1 :NUMBER2)
14
;
; Multiplication operation. Output result using SHOW command
SHOW :NUMBER1 * :NUMBER2
851
; Multiplication operation using PRODUCT command.
SHOW (PRODUCT :NUMBER1 :NUMBER2)
851
;
; Define two variables “DIVIDEND” and “DIVISOR”.
MAKE "DIVIDEND 24
MAKE "DIVISOR 6
; Division operation. Output result using SHOW command
SHOW :DIVIDEND / :DIVISOR
4
; Division operation using QUOTIENT command.
SHOW (QUOTIENT :DIVIDEND :DIVISOR)
4
;
;
; Now output results using PRINT Statement.
; Define variable “RESULT” as product of two variable “NUMBER1” and “NUMBER2”.
MAKE "RESULT :NUMBER1 * :NUMBER2
PRINT :RESULT
851
;
; Define variable “ADDITION_RESULT” as sum of NUMBER1 and NUMBER2.
MAKE "ADDITION_RESULT :NUMBER1 + :NUMBER2
PRINT :ADDITION_RESULT
60
;
; Define variable “DIVISION_RESULT” as result of division
MAKE "DIVISION_RESULT :DIVIDEND / :DIVISOR
PRINT :DIVISION_RESULT
4
; Define variable “SUBTRACTION_RESULT” as difference of NUMBER1 and NUMBER2.
MAKE "SUBTRACTION_RESULT :NUMBER1 - :NUMBER2
PRINT :SUBTRACTION_RESULT
14
LOGO Graphics Using Variables and make Command
We did a few LOGO graphics programming examples on the Parameters page. Le’s now extend these programs to do graphics using LOGO variables and the make
command.
Draw a Spiral
The program below draws a spiral.
PROGRAM EXAMPLE: LOGO GRAPHICS USING VARIABLES (MAKE COMMAND)
CLEARSCREEN
SETPENCOLOR 4; Set pen color RED.
HIDETURTLE
PENUP; Lift pen up so the turtle does not draw when moving to (-100,100)
SETXY -150 150
PENDOWN; Set pen down to enable the turtle to draw.
MAKE "ANGLE 7; 1) Define a variable ANGLE and assign value to it 10.
;
MAKE “DISTANCE 3 ;2)
; On every REPEAT iteration, increase the value of
; variable ANGLE by 1 degree.
; And increase the DISTANCE variable by 2. ;3)
REPEAT 25[CIRCLE 4 FORWARD :DISTANCE MAKE “DISTANCE :DISTANCE + 2 RIGHT :ANGLE MAKE "ANGLE :ANGLE + 1] ;3)
Detailed Description of the Program
Step 1): We define a variable “ANGLE and assign it a value of 7.
Step 2): We defined another variable “DISTANCE. We use the make
command to assign it a value of 3.
Step 3): The REPEAT statement increases the value stored in variable ANGLE by 1 degree at each iteration of the REPEAT by using the command “ANGLE :ANGLE + 1. Thus, at every iteration of the REPEAT statement, the angle by which the turtle turns is increased by 1 degree.
Step 3): At each iteration of the REPEAT command, the distance by which the turtle moves is increased by 3 pixels.
In Step 3), the REPEAT loop is repeated 25 times. The resulting picture drawn by the turtle is a spiral.
Run this program on your computer to view the resulting shape. Try incrementing the angle by a different amount and see how the shape changes.
The following is the program’s output.
Let us now create another graphics using the make
command and the RANDOM command.
LOGO Graphics Using Variables and Random Command
We can get interesting results if we use the RANDOM command discussed on the “LOGO Additional Commands” page to change the value of the variable to a new random value at each successive iteration of the REPEAT statement.
LOGO GRAPHICS WITH MAKE COMMAND AND RANDOM COMMAND
CLEARSCREEN
PENUP
SETXY -150 150
PENDOWN
HIDETURTLE
REPEAT 5[ ;Repeat the loop within the brackets 5 times.
SETPENCOLOR RANDOM 16; Step 1) Select a random color from the Color Index Table.
SETPENSIZE RANDOM 8; Step 1) Select a random pen size width between 1 and 8.
MAKE "SIDE 100; Define a variable SIDE and assign a value 100.
MAKE “ANGLE 0; Define a variable ANGLE and assign a value 0.
REPEAT 3[FORWARD :SIDE RIGHT 120]; Step 3) Draw a triangle.
RIGHT :ANGLE +70 ; Step 4)
]
Detailed Explanation of the Program
Step 1): We randomize the pen color to a value between 0 and 15 and the pen width between 1 and 8.
Thus, at each iteration of the REPEAT statement, a new random value of the pen color and pen size width is used.
The program defines two variables “SIDE and “ANGLE.
Step 3): Draw a triangle of side length = :SIDE.
Step 4): The :ANGLE value is increased by 70 degrees at each successive iteration of the REPEAT loop
and a new pen color is used for each REPEAT loop.
The REPEAT command loops five times.
Run this program on your computer several times; you will get different colors and different pen widths each time.
The following is program’s output.
Copyright © 2019 – 2022 softwareprogramming4kids.com
All Rights Reserved