Kanwal Rekhi School of Information Technology, IIT Bombay



Highlighted:

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

Let us talk about Angle :

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

RT 180 makes the turtle take half a turn

RT 90 makes the turtle take a quarter turn

We can turn the turtle in different directions as shown in figure.

I have used turnright as RT and turnleft as LT.

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

DRAW A FIG ALSO

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.

|Command |Description |Example |

|PENUP |Turtle moves without drawing a line|PENUP ou PU |

|PENDOWN |Put the turtles pen back down on |PENDOWN or PD |

| |the paper. | |

|HIDE TURTLE (HT) |Hide the turtle (triangle). |HT |

|SHOW TURTLE (ST) |Show the turtle (triangle). |ST |

|CLEARSCREEN (CS) |Clear the screen and start over. |CS |

|FORWARD(FD) |Move the turtle forward by |FD 100 |

| |specified number steps. It draws a| |

| |line as it moves. | |

|RIGHT TURN(RT) |Turn the turtle to the right. The |RT 90 |

| |number defined tell the turtle how | |

| |much to turn | |

|LFET TURN (LT) |Turn the turtle to the left . The |LT 90 |

| |number defined tell the turtle how | |

| |much to turn | |

|BACKWARD (BK) |Move the turtle backwards 100 |BK 100 |

| |steps. | |

|HOME |Turtle goes back to its starting |HOME |

| |position | |

|PENERASE(PE) |Makes the turtle erase |PENERASE or PE |

|SETH |Set the turtle head in specific | |

| |direction | |

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.

Cut these following lines---------

A procedure is useful if we want to reuse a certain set of commands. We can give a name to a set of commands and can recall it when needed. A procedure once defined can be stored in memory and can be called whenever required.

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 Title of the Procedure

* Can include letters, numbers or symbols

* First character should always be a letter

* Blank space can not be included

* Arithmetic operators(+,_,*,/) can not be included

* LOGO command names can not be included

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

Procedure with Parameters

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.

Now execute the above procedure with

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.

Now Execute with

SQUARE 75

It will make a square of side 75 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.

|Polygon |No. of Sides |Command |Angle(RT) = 360/No.of Sides |

|Triangle |3 |REPEAT 3 [FD 50 RT 120] |360/3 = 120 |

|Square |4 |REPEAT 4 [FD 50 RT 90] |360/4 = 90 |

|Pentagon |5 |REPEAT 5 [FD 50 RT 72] |360/5 = 72 |

|Hexagon |6 |REPEAT 6 [FD 50 RT 60] |360/6 = 60 |

You can draw a polygon of N number of sides with an angle 360/N.

Therefore the Procedure for drawing a Polygon will be

TO POLYGON :N :X

REPEAT :N [FD :X RT 360/N]

END

Here X is the length of sides.

Try increasing the number of sides and you will find that Polygon will look like a circle.

Execute the following command and see what happens:

REPEAT 36 [ FD 1 RT 10]

Do you get a circle in the screen?

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.

Now You can write the procedure for the circle.

Some More commands related to Procedure

| Command |Description |Example |

|ERASE “procedurename |Delete the procedure with the given|ERASE “SQUARE |

| |procedurename, | |

|SAVE “procedurename | Saves the procedure with the given|SAVE “SQUARE |

| |procedurename permanently in hard | |

| |disk | |

|LOAD “procedurename |Recall the procedure and use it |LOAD “SQUARE |

| |again | |

|POTS |Lists all the procedures stored in | |

| |memory on the screen | |

More About LOGO

Till Now we have learned the drawing part of LOGO. Now we will learn about words and numbers in LOGO.

PRINT(PR) COMMAND

PRINT Command is used to print any number or text on the screen.

PRINT “HELLO will print a word HELLO on the screen. Double Quote “ is must before the word.

PRINT 45 will print number 45 on the screen.

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

To run this procedure we will write;

GREET “Student

the output will be,

HELLO, Student

WELCOME TO LOGO PROGRAMMING CLASS.

Exercise:

1) Write a procedure to print your name.

2)

We can also perform arithmetic operations using PRINT command.

PRINT 3+5 will display the answer 8

PRINT 30/3 will display answer 10

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 2 |SUM 5 3 |

|DIFFERENCE |Number1 Number2 |Subtracts Number2 from Number1 |DIFFERENCE 8 5 |

|PRODUCT |Number1 Number2 |Multiply Number1 by Number2 |PRODUCT 3 4 |

|QUOTIENT |Number1 Number2 |Divides Number1 by Number2 |QUOTIENT 30 3 |

|RAMDOM |Number |Produce a number greater than or equal to 0|RANDOM 50 |

| | |and less than Number | |

How Operators Work

Lets take a simple example.

PRINT SUM 5 8

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.

Take an another example with two operators.

PRINT SUM 4 PRODUCT 10 2

LOGO will take following steps to evaluate this instruction.

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

Try writing procedures to find area of Triangle and circle.

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.

Words and Lists

Make a list of what all is there in your school bag.

Textbooks, notebooks, lunchbox, pencilbox, crayons etc..

This is a list of items and each item is a part of this list.

We store the list in [ ] with space in between each member.

[textbooks notebooks lunchbox pencilbox crayons ]

Now Suppose we want to print different items from the list.

To print the first item we write the logo command as follows.

PRINT FIRST [textbooks notebooks lunchbox pencilbox crayons ]

The result you will see is 'textbooks'.

FIRST is an operation that takes one input. The output from FIRST is the first member of the input if the input is a list, or the first character if the input is a word. Try this.

PRINT FIRST “Hello

The output will be the first character H as the input is a word.

Similarly we can print other commands. The list of commands is shown in the table below.

|command |Description |Full syntax |example |output |

|FIRST |Will print the first |PRINT FIRST list/word |PRINT FIRST [textbooks notebooks | textbooks |

| |item from the list or | |lunchbox pencilbox crayons ] | |

| |word | | | |

|LAST |Will print the last item|PRINT LAST list/word |PRINT LAST [textbooks notebooks |crayons |

| |from the list or word | |lunchbo pencilbox crayons ] | |

|BUTFIRST |Will remove the first |PRINT BUTFIRST list/word |PRINT BUTFIRST [textbooks notebooks |[notebooks lunchbox |

| |item from the list or | |lunchbox pencilbox crayons ] |pencilbox crayons ] |

| |word | | | |

|BUTLAST |Will remove the last |PRINT BUTLAST list/word |PRINT BUTLAST [textbooks notebooks |[textbooks notebooks |

| |item from the list or | |lunchbox pencilbox crayons ] |lunchbox pencilbox] |

| |word | | | |

|ITEM |Will select the |PRINT ITEM number list/word |PRINT ITEM 3 [textbooks notebooks |lunchbox |

| |specified item from the| |lunchbox pencilbox crayon] | |

| |list or word. It takes | | | |

| |two inputs. | | | |

We can print any item from the list using the combination of these commands.

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 |

The details of all these commands are as follows.

RC command:

We can only give a single digit number in this command.

TO ADD

PR 10 + RC

END

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.

PR[What is your name]

PR RW

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”.

Comparison with SCRATCH:

Last year we learned SCRATCH where we can click and drag in order to create sequences of code that do things like make a character move or trigger a series of events. Now we will draw shapes by steering a turtle around a screen by typing out the commands.

-----------------------

RT 45

RT 90

RT 135

RT 180

LT 180

LT 135

LT 90

LT 45

Turtle at the beginning

Turtle after turning

360

Turtle at the beginning

180

Turtle after turning

Turtle at the beginning

90

Turtle after turning

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download