Professional Documents
Culture Documents
Highlighted things are already there in the existing LOGO chapter written.
Next let us say we want the turtle to turn right and then move another 50 steps. We then
write in the left pane turnright 90, where 90 represents the angle in degrees that the
turtle has to turn, and again forward 50 to get the following.
FIG is there ---- AFTER THIS
Angle is basically changing the direction of a turtle by turning it to the right or left. Arrows are
showing the turtle 's direction.
RT 360 makes the turtle take a full turn
360
180
Turtle at the Turtle after turning
beginning
90
LT 45 RT 45
LT 90 RT 90
LT 135 RT 135
RT 180
LT 180
Now, let us say we want to use what we have learnt to draw a square. We will abandon the
current canvas and start afresh by using the reset command. The following screenshot
shows the program and the square drawn by the turtle.
FIG screenshot with code
RESET
FORWARD 50
RT 90
FORWARD 50
RT 90
FORWARD 50
RT 90
FORWARD 50
Repeat Command: Many times we repeat the same command to draw an object. For example,
while drwaing Square in a previous example, we gave the following commands 4 times as shown in
the example.
FD 50 RT 90
We can make the same square by using REPEAT command. In REPEAT command, we just have
to tell how many times we want to repeat the command. For Example,
REPEAT 4 [FD 50 RT 90]
Here 4 indicates the number of times we want to repeat the command. The commands which have
to be repeated are enclosed in the square brackets [ ].
The REPEAT command is very useful in drawing shapes. Now try the following commands and
find out what shape you get on the screen.
CLEARSCREEN
FD 50
RT 120
FD 50
RT 120
FD 50
RT 120
I think you get a Triangle. A Triangle has 3 sides so FD 50 RT 120 commands are given 3 times.
We can draw the same triangle using REPEAT command.
CLEARSCREEN
REPEAT 3 [FD 50 RT 120]
Simple Keywords:
So far we have used the FORWARD,RIGHT TURN, RESET and REPEAT command. Few more
simple commands are listed below.
Exercise
1. Give the correct command
i. Move ahead 50 turtle steps ------------------
ii. Right turn by 60 ------------
iii. Clear the screen -------
2. Write the command to draw the following.
i.
ii.
Procedures
What are Procedures:
Suppose you want to make a phone call to you friend. Think, What all you are going to do..
Here are the Steps:
1. Get the phone number of your friend
2. Pickup the receiver,
3. Dial the phone number
4. Wait for the phone to be picked up
This is a procedure for making a phone call. It contains several steps.
Same way we can write procedures in LOGO.
In Logo, the commands are executed when entered. These commands do not get stored in memory.
Till now we have been writing commands one by one to draw a figure. If the same figure is to be
drawn many times, the same set of commands have to be written again. Instead if we write the
commands and give it a name, and use the name wherever we want. We can do it with the help of
Logo Procedures.
A Procedure is a set of LOGO commands given one after another to perform a particular task and
referred to by a name.
A procedure has 3 parts:
1. It must start with the reserved word “TO”, followed by a one word procedure title.
2. The main body is made up of set of commands
3. The procedure ends up with word “END”
The name of the procedure should be such that it gives some idea about the set of instructions.
We have drawn a square using a REPEAT command. Now we will write it as a procedure.
TO SQUARE
REPEAT 4 [FD 100 RT 90]
END
To execute the above procedure, type SQUARE at your LOGO prompt and press enter key. You
will see a square is drawn on the Logo screen.
Let us write another procedure to draw a rectangle.
TO RECT
CS
FD 50
RT 90
FD 80
RT 90
FD 50
RT 90
FD 80
RT 90
HT
END
Exercise: To be done
We used the procedure SQUARE to make a square of size 100 turtle steps. Now if we want to
make another square of size say 80 turtle steps then we have to write another procedure. By using
the parameters, we can give the size while executing the procedure.
By using the parameters, we can use one procedure to draw squares of different sizes. Here is an
example. Type the following
TO SQUARE :SIZE
REPEAT 4 [FD :SIZE RT 90]
END
Here we have put 'SIZE' instead of putting the number. This 'SIZE' is a parameter. The colon(:)
before the 'SIZE' tells the computer that 'SIZE' is a parameter. The value of the 'SIZE' can be given
when we execute the procedure. The Computer will use that value of 'SIZE' in place of the
parameter inside that procedure.
SQUARE 50
Here SQUARE is a command and 50 is a value of parameter “SIZE”. The computer will make a
square of side 50 turtle steps.
We can also specify more than one parameters. Suppose we make a rectangle of different sizes. To
draw a rectangle we need to know the Length and Breadth. By using Parameters we can specify
both Length 'L' and Breadth 'B'.
TO RECT :L :B
FD :B RT 90
FD :L RT 90
FD :B RT 90
FD :L RT 90
END
You can execute this procedure by giving values 25 and 50 to draw a rectangle of length 25 and
breadth 50. Type RECT 25 50 and see. You can give different numbers for L and B and try.
We can use procedures inside the other procedure. See an example. Here we are using a procedure
SQUARE inside procedure FACE 4 times.
TO FACE
PENDOWN SQUARE 100
PENUP FD 20
RT 90
FD 25
PENDOWN FD 50
PENUP BK 75
LT 90
FD 65
RT 90
FD 20
PENDOWN SQUARE 15
PENUP FD 45
PENDOWN SQUARE 15
PENUP BK 15
RT 90
FD 20
LT 45
PENDOWN SQUARE 20
END
Drawing a Circle:
Till now we have read about how to draw a triangle, a rectangle and a square. These closed figures
are called Polygons.
You can draw polygons of different number of sides by using the following table.
REPEAT 36 [ FD 1 RT 10]
In this command what we have done is we have increased the number of sides by 36 and reduced
the length of the side by 1 turtle step and hence the polygon looks like a circle.
PRINT(PR) COMMAND
PRINT [ Enjoy LOGO Programming] will print the sentence – Enjoy LOGO Programming - on the
screen. Square Brackets are must if more than one word or number are given.
We can write procedures to print some messages. Let us write a procedure to greet a person with
any name.
We will give the greeting message like: Hello, “person's name”. Welcome to LOGO programming
class.
TO GREET :PERSON
PRINT “HELLO, :PERSON
PRINT [ WELCOME TO LOGO PROGRAMMING CLASS.]
END
HELLO, Student
WELCOME TO LOGO PROGRAMMING CLASS.
Exercise:
Operators:
The following are the most common maths operators. They are the procedures already written in
the LOGO. We can directly use them. Try each of them with PRINT command.
Operators Inputs Description Example
SUM Number1 Number2 Adds Number1 and Number SUM 5 3
2
DIFFERENCE Number1 Number2 Subtracts Number2 from DIFFERENCE 8 5
Number1
PRODUCT Number1 Number2 Multiply Number1 by PRODUCT 3 4
Number2
QUOTIENT Number1 Number2 Divides Number1 by QUOTIENT 30 3
Number2
RAMDOM Number Produce a number greater RANDOM 50
than or equal to 0 and less
than Number
PRINT SUM 5 8 will display the answer 13. The input to PRINT is the expression SUM 5 8.
Logo evaluated the input before passing it to the PRINT command. This means that Logo invoked
the necessary procedures (in this case, SUM) to compute the value of the expression . SUM requires
two inputs. In this case we gave it the numbers 5 and 8 as inputs. The task of operator SUM is to
add two numbers. It is the result of this addition, the output from SUM, that becomes the input to
RRINT.
PRINT OUTPUT
OUTPUT = SUM number1 number2
number1 = 4
number2 = outputfromproduct
outputfromproduct = PRODUCT 10 2
Explanation:
The first thing in the instruction is the command PRINT . Logo knows that PRINT requires
one input, so it continues reading the instruction line.
The next thing Logo finds is the word SUM. This is an operator. This tells Logo that the
output from SUM will be the input to PRINT.
Logo knows that SUM takes two inputs, so it will check for the two inputs.
The next thing in the instruction is the number 4, so that must be the first input to SUM. The
value of this input is 4.
Logo needs the second input to SUM. The next thing in the instruction is the word
PRODUCT. This is, again, an operator. Logo must evaluate this operator to get the SUM's
second input.
Logo knows that PRODUCT requires two inputs. It must now look for the first of those
inputs. (Meanwhile, PRINT and SUM are both "on hold" waiting for their inputs to be
evaluated. PRINT is waiting for its single input; SUM, which has found one input, is waiting
for its second.) The next thing on the line is the number 10 which is the first input to
PRODUCT.
Logo continues reading the instruction. The next thing it finds is the number 2. This
number is the second input to PRODUCT.
Logo is now ready to invoke the operator PRODUCT, with inputs 10 and 2. The output from
PRODUCT is 10 times 2, or 20.
This output, 20, is the value of the second input to SUM. Logo is now ready to invoke SUM,
with inputs 4 and 20. The output from sum is 24.
The output from sum, 24, is the input to PRINT. PRINT will now print the output 24.
Using these operator you can also find area of Square by writing a procedure.
TO AreaSquare :L :B
PR PRODUCT L B
END
RANDOM Operator: This operator picks up a random number between 0 and one less than the
input. Do you know what is the use of this operator.
Suppose your teacher wants to select a monitor in your class. Any one can become a monitor and
total number of students are 30. Teacher has to pick the student randomly. She will make 30 chits
with your roll number /name written on them. Teacher will mix all the chits in a box and then pick
one of them.
This can be done very fast using RANDOM operator. We just have to write
PRINT RANDOM 30 will pick number between 0 and 29. So instead of RANDOM 30 we will
give command.
PRINT 1 + RANDOM 30
This will generate a number between 1 and 30.
The student with that roll number can be selected.
Using REPEAT we can select more random numbers also
like REPEAT 5 [ PR 1 + RANDOM 30]
It will display 5 random numbers between 1 and 30.
Similarly we can print other commands. The list of commands is shown in the table below.
Naming a List
It is not convenient to give a list every time we want to use a list. In place, we can define a list and
give a name to it. Let us give a name to our list as SchoolBag. The command to give a name to a
list is MAKE. MAKE command saves the inputs in the computer memory.
MAKE “SchoolBag [textbooks notebooks lunchbox pencilbox crayons ]
Now we can use the name SchoolBag in place of giving the list. Let us try with above mentioned
commands in table.
PRINT FIRST :SchoolBag
colon (:) is put for computer to identify that it is a list.
We can also add an item in the list in the beginning or at the end using following commands.
FPUT: to add an item at the beginning of the list
LPUT: to add an item at the end if the list
Examples:
LPUT “waterbottle [textbooks notebooks lunchbox pencilbox crayons ]
This command will add 'waterbottle' at the end. Now the list will be
[textbooks notebooks lunchbox pencilbox crayons waterbottle]
Input Commands:
LOGO has some commands to read input from the keyboard. It can accept a number, a word or a
list from the keyboard.
command description
RC Inputs a single digit input
READ or RW Inputs abigger number or word
RL Inputs a list of words or numbers
When we execute this program, computer will wait for us to input a number. We enter 5 then it will
give the result as 15. We don't have to press enter after giving input.
RW Command
We can give big number or words in this command.
TO ADD
PR 10 + RW
END
WE give input as 50,the result we get will be 60.
Computer will wait for input and as we type the name and press enter the result will be shown.
RL Command
We can input a list of words or numbers as input. For example
PR [Write Name of five games ]
PR RL
When you run this program, massage “ Write Name of five games” on the screen and computer
will wait for input. You can enter words and press enter key. These words will form items in the
list.
Conditions in LOGO
Suppose Your Mom said You can watch TV only if you finish your work. Here in this sentence,
the condition is put. The condition is—finish your work. Once the condition is matched you can get
to watch tv. Same way in LOGO we can check conditions. We do it using IF-THAN-ELSE. This
command instruct the computer to check the condition and then execute the command depending on
the result of the check. Consider the following example.
PR [type your age ]
MAKE “ AGE RW
IF :AGE >10 THEN PR[You are older than me] ELSE PR[You are younger than me]
Here You enter your age and computer stores it in a variable called AGE. Then computer compares
the age with 10. If the age is greater than 10 then computer prints a message “ You are older than
me”. If the age is less than 10 or equal to 10 than it will print the message “You are younger than
me”.