In this article you will learn what is Logo Programming and its commands.
- What is Logo?
Logo is an easy and simple programming language. It is used to teach students and children how to program on a computer.
- Where did Logo come from?
Logo language was derived from another programming language LISP. Logo word is derived from the Greek logos, meaning word or thought.
- When and who designed Logo?
It was designed in 1967 by Wally Feurzeig, Seymour Papert, and Cynthia Solomon.
- What’s turtle?
- In 1940s turtles were educational robots designed for training purposes. Logo could control such a robotic turtle. The robot turtle walked around on a piece of paper. It held a pen on the paper and it drew lines as it walked around.
- It was expensive to make robot turtles and hook them up to computers. Once monitors came in and became cheaper, Logo controlled a pretend turtle robot on the computer screen instead of a real robot turtle. Today the “turtle” is a small triangle or arrow head that moves on the screen of a computer.
- What can Logo do?
With Logo you can
- Draw simple shapes and designs.
- Do arithmetic calculations such as addition, subtraction etc.
- Why do I need to learn Logo?
- Logo is simple, easy and fun language.
- It helps to get good understanding of the basics of computer languages.
- It also helps understand basics of geometry like angles, graphs taught in Math.
- Where do I get Logo?
There are many different versions of logo. Two common versions of logo are UCBLogo, and MSWLogo. You can Google on it and both of them are free and downloadable from internet. Once you download and install it will create a shortcut icon on your desktop. Alternatively you can just type it on search bar to locate it.
- How does Logo screen look like?
When you click on Logo icon two windows opens up which look like below. The two windows are called
- Graphic Screen
- Command Window
- What is the Graphic Screen?
The graphic screen typically appears on top of the command window. It is the area where we draw our pictures or turtle moves with its pen. It has following parts
- Title Bar: On the top of the window is the title bar which has the MSWLogo icon and Name on the left side and minimize, maximize and close button on the right side.
- Menu Bar: Below title bar is the menu bar which has the choices of File (Has options to save and load our programs or procedures), Bitmap (Has options to load, save, print the pictures we create), Set (Has options to change the font, pen size, colors etc.), Zoom to zoom the screen in or out and Help (Has options to assist if you need any help).
- Turtle: The small triangle at the center of the graphic screen is called the Turtle. The top pointed end of the turtle is called its head and wide base at bottom is called its tail. We draw on the screen by typing in commands to control this turtle.
- What is the Command Window?
The command window typically appears below the graphic screen. This is where we give the commands to control the turtle. It has following parts.
- Title Bar: On the top of the window is the title bar which has the MSWLogo icon and Commander Name on the left side and minimize, maximize and close buttons on the right side.
- Command Box: It is also called as Recall List or Output box. This box lists or stores all the commands that we have entered. It also shows the results of our commands such as arithmetic operations.
- Input Box: This appears below the command window and this is the area where we type our commands.
- Control Buttons: There are 8 buttons on the command window on the right hand side. Many of them are toggle buttons (on/off kind). We will use some of them in your course.
- Halt: To stop the action of turtle
- Trace: Turn debugging on or off
- Pause: To stop the action of turtle temporarily
- Status: To display/hide the status window.
- Step: To turn off/on single step for debugging.
- Reset: To erase everything on the screen and to bring the turtle back to home position.
- Execute: To execute or run the command
- EdAll: It brings up the editor window to write set of commands.
- What is home position?
When we start Logo, the turtle is at the center of the graphic screen. This position of turtle is called as the home position of the turtle.
- What is the size of the graphic window?
- The Logo drawing screen is actually like a graph and the turtle is sitting at the center of the graph where X and Y axis meet.
- The turtle can move 250 steps on all 4 sides from the home position. If you make it move more than 250 steps on either side it wraps around. The right side wraps to the left side and the top wraps to the bottom.
- The head of the turtle gives the direction in which it is set to move. The direction can be changed by using the RT and LT command. The picture shows how to make the turtle in different direction from its home position.
- How do we exit logo?
To exit logo, you can either –
- Type bye in the input box and press execute.
- From File menu choose the exit option.
- Click on close button on the graphic window title bar.
Day 1: Clean up commands
Before we start writing any of the commands, let’s learn what we can do in case any of our commands go wrong. You can type the full command or their abbreviation. For all of the commands their abbreviation will also be shared.
- Press Reset button on the command window.
It erases everything on the screen and brings the turtle back to home position.
- CLEARSCREEN (or CS)
This command clears the screen and returns the turtle to it’s home position.
- CLEARTEXT(or CT)
This command removes the text or commands from the command window and cleans it up.
This command is used to exit logo.
Basic Movement Commands
First we will learn simple drawing commands which will move the turtle backward, forward and turn it left or right. Each of these commands takes in one argument which is no of steps to move or angle with which to turn.
- FORWARD (or FD) x
Forward means moving ahead. It moves the turtle forward by x pixels in the direction of its head.
Example: FD 50
- BACK (or BK) x
This command moves the turtle backward x pixels in the direction of its tail without moving its head.
Example: BK 50
- LEFT (or LT) x
This commands rotates the turtle x degrees left. The turtle heads moves left by x degrees.
Example: LT 90
- RIGHT (or RT) x
This command rotates the turtle x degrees right. The turtle heads moves right by x degrees.
Example: RT 90
Logo has some set of commands to manage the pen while drawing.
- PENUP or PU
Short for “pen up,” it lifts the “pen” from the screen so that moving the turtle doesn’t draw a line. It is helpful when you need to move to a different place to draw on the screen.
- PENDOWN or PD
Short for “pen down,” it puts the pen down so that moving the turtle draws a line.
- SetPenSize [x x]
This command is used to set the thickness of the pen. It sets the width of the pen to x pixels. You have to put the SAME number twice.
Example: SetPenSize [5 5]
- SetPenColor or SetPC colorno or SetPC [r g b]
This command is used to set the pen color to RGB (Red, Green, Blue) values, where r, g, and b are numbers that range from 0 to 255. Or to set it to given color no.
Example: SetPC [255 0 0] SetPC 4
- SetScreenColor SetScreenColour SetSC colorno or SetSC [r g b]
This command is used to set the background color to RGB (Red, Green, Blue) values, where r, g, and b are numbers that range from 0 to 255.
Example: SetPC [255 0 0]
- Penerase (or PE)
This command sets the pen mode to erase like it becomes an eraser. When the pen is moved, it will erase whatever is under it.
- Pennormal, Penpaint, PPT
This command sets the pen back to normal writing mode. When the pen is moved, it will write normally.
0 black 1 blue 2 green 3 cyan
4 red 5 magenta 6 yellow 7 white
8 brown 9 tan 10 forest 11 aqua
12 salmon 13 purple 14 orange 15 grey
Turtle Position Commands
There are set of commands to manage the turtle when you are drawing.
- HT or HideTurtle
This command is used to hide the turtle. However any move commands will still leave a line.
- ST or ShowTurtle
This command is used to show the turtle. However any move commands will still leave a line.
This command is used to bring the turtle back to its home position. It will come back to center and head will also point in straight up position.
- SetPOS [x y]
This command is used to set the absolute x and y position of the turtle. If the pen is down, it will draw a line from it’s previous position.
Example: setpos [50 100] Takes the turtle to x=50 and y=100
- PRINT or PR
This command is used to print text, numbers in command window. For single word you need to put single QUOTE (“) ONLY in the beginning. For printing a complete sentence you need to enclose them in square brackets [ ]. For numbers you can write directly.
Example: PRINT “Oneword PRINT [Hi! How are you?] print 4
- Arithmetic Operators SUM or +, DIFFERENCE -, QUOTIENT /, PRODUCT *
The addition (+), subtraction (-), division (/) and multiplication (*) operators can be used with any of the commands which take in number inputs. You need to put in space after minus otherwise it is treated as –ve number. E.g. print 10 -7 will give an error.
Example: PRINT 2 * 4 PRINT 4 – 2 fd 40 + 10 PR SUM 2 3, PR DIFFERENCE 4 2
- Comments (;)
If we need to add any comments to our program which are not to be printed on screen but is for our understanding then we use semi-colon. Anything that is typed after ; is ignored by Logo.
Example: PRINT 2 * 4 ; this will multiply the nos
Many times we have the repeat the same instructions many times. This is also called as looping. Repeat command helps us to write compact code and makes it faster too.
- REPEAT n [instruction list]
This command repeats the action listed in the instruction list n number of times.
Example: Repeat 4 [fd 40 lt 90] creates a square
In some situations especially gaming, you need to generate random numbers, for e.g. random positions where your target will appear or in drawings where you want random drawing on the screen. Or to play dice or tic-tac-toe.
- RANDOM n
This command returns an integer random number that ranges from 0 to n-1.
Example: Random 7 ; gives a number from 0 to 6 like a dice
Variables are used to store values that can be referred or used later. In computers there are different memory addresses which store values. Since it is difficult to remember addresses, users are allowed to give it a name of their choice.
To create a variable and give it a value we use the make command.
Example: Make “size 100
Now to use size we use a colon (:) in front of it.
Print :size – 10
Program or Procedure Writing
Many times we want to use a block of code or instructions again and again. In computer world this is called as reusability of code. Procedures provide a way for us to group multiple commands and use it like any built in command like print etc.
- TO … END
To write a procedure we put the block of code in between TO and END command like below. After this we just have to write the name of the procedure to use it.
block of code
Example: TO SQUARE
REPEAT 4 [ fd 100 rt 90 ]
To use the procedure we just write the procedure name
If you want to view the procedure code, you can use the PO command to view the instruction written in a procedure.
Example: PO “procedure_name or PO [procedure_name]
Many times you have to correct or change a procedure or enhance an existing procedure. You can open a previous procedure with the help of edit command.
Example: EDIT “procedure_name or EDIT [procedure_name]
When you press enter the logo editor opens up where the existing code of the procedure is displayed. Once you make the change you can save the new code by selecting File-> Save and exit to return back to command window
POTS or Print Out Titles is used to display the list of all procedures saved on the disk.
- ERASE or ERASE ALL
This command is used to erase the procedure from the disk. You can also use ERASE ALL to erase all procedures you have saved.
Example: ERASE ‘SQUARE or ERASE [SQUARE]
Procedures can also be written inside another procedure. Then they are called as sub procedures.
Passing parameters to procedure
You can also pass parameters to a procedure. Parameters are nothing but variables which can have different value passed. Like built in command fd takes a number variable as parameter.
In such a case we use :varable name after procedure name like how we use variables. For e.g. use this program to draw a square of given size.
to square :size
repeat 4 [fd :size rt 90]
Now to use the procedure we will give