All programs should use the following syntax:
to <insert name of program here> ... end
This defines the program, so when you type the name of the program it will execute.
In the following examples I will tend to not insert to and end. You will need to insert these in order for your program to work.
This will draw a straight line that is 100 "units" long.
To rotate the turtle, use right or left. To go back, use back.
The Basic SquareEdit
So, using forward and right, we can draw a square by using this code:
forward 100 right 90 forward 100 right 90 forward 100 right 90 forward 100 right 90
So far, we have managed to draw a square, but note how many lines it took! A square is a common shape, so it should be done in a matter of seconds. Clear the screen using clean.
Look back at the code, and you will find that it is forward 100 right 90 for four times. If you want to repeat something, use the repeat function.
repeat 4 [foward 100 right 90]
This tells the computer to go forward one hundred steps and then turn right ninety degrees, and to do this four times. We get the same square, but in one line.
Instead of using the long words forward, right and back, we could use fd, rt and bk. This shortens the program to
repeat 4 [fd 100 rt 90]
So far, we have a program that does something when executed. Suppose now that you wanted a program that draws different sizes of squares, as dictated by the user.
to square :size repeat 4 [fd :size rt 90] end
Here, we have made a program that has a variable input, :size. To execute this program we type square x, replacing x with a number.
Note that the program square is used in the same way as commands like fd are used. This is because fd and the like are actually programs built in to the LOGO library.
So far, we know the code to make a square, and also by deduction we can work out the code for triangles, hexagons, et cetera with different sizes.
to triangle :size repeat 3 [fd :size rt 120] end
to hexagon :size repeat 3 [fd :size rt 60] end
This is inefficient, because for each polygon you need a new program. Why not instead have a program that you can tell how many sides you want, and how long the sides are?
There is such a program, and it uses variables. You will need two variables: one for the length of sides, and another one for the number of sides.
to polygon :size :sides repeat :sides [fd :size rt 360/:sides] end
In the above example, the program will draw a number of lines accoring to the variable :sides, as represented by the repeat :sides. The lines are at a length of :size and are at an angle of 360/:size to each other, as the external angles always add up to 360.
So, to draw a pentagon of size 100, you type polygon 100 5.
That covers most of LOGO basics. Now, we will move on to using the console.
MSWLogo also allows you to use the console screen for programs involving counting and printing. For example:
print [hello, world]
yields the phrase "hello, world".
Variables are assigned using make and name.
make "a "hello show :a
make "a 123 fd :a
In the first program, the computer will store the variable "a as the word hello, and displays its value when this program is run. In the second program, the computer stores the same variable as the number 123, and then moves the turtle that many steps. If we had replaced 123 with another number, then fd would use that other number.
So far, we can make a program that displays texts and numbers and moves the turtle, and we have met the
repeat function. The
repeat function is an example of a control statement, a function which repeats, omits, or otherwise moves the computer's "eyes" to look at another line of code.
to check :a if :a = 1 [print [a equals one]] print [OK, it has been checked.] end
This program displays the syntax for if (the most common control statement next to repeat), which decides whether to perform a set of instructions depending on a variable. Here, the computer checks whether "a equals one or not, and if so, prints out the string a equals one. It then prints out OK, it has been checked. regardless of the value of "a.