Chapter 3 Lab
Chapter 3 Lab
Variables
A variable is the name of a memory location which can contain a value. In a computer,
each memory location has an integer address. Since it would be hard to remember the
address of each location containing a value used by a program, computer scientists
have found ways of giving these locations, symbolic names. Once a variable has a
name, we can use and manipulate it.
Variables are given names which are strings of letters. A variable name can contain
alphabets (case insensitive), digits and underscore. A variable name can be accessed
in a computation using ‘:’ before it. Let us consider the following example in the
screenshot.
Arithmetic operators have a precedence that determines the order with which they are
evaluated.
Note − print 60 * sqrt 2 and print sqrt 2 * 60 produce different answers. Here the *
operator has a precedence over the sqrt operator. Thus, * will be done before sqrt, if
there is a choice, as there is in the second case.
For this reason, the first statement prints the value of 60 times the square root of 2,
whereas the second statement prints the square root of 120 as shown in the following
screenshot.
Logo – Repetition/Loops
We often repeat a sequence of commands. Computer programs often perform repetitive
tasks. Just about every programming system has a way of carrying out this repetition, or
iteration, as computer scientists call it. Let us consider the following example −
Let us assume we want to draw a square with sides of length 100, we can do this with
the following program −
fd 100
rt 90
fd 100
rt 90
fd 100
rt 90
fd 100
rt 90
We note that the two commands – fd 100 and rt 90 are repeated four times. Will it not
be simpler to tell the computer that it should just repeat these two commands four times
instead of writing them four times in a row? We can do exactly this, using the following
command −
It saves our time of typing-in to make a square. The general form is: repeat number
[commands]. We must use the keyword – repeat followed by a number and then a
sequence of commands in [square brackets].
Often, we might have to repeat within repeat. This is called nesting. Let us look at
some examples on this.
Following is an exercise to check your aptitude on what you have learnt so far in this
chapter.
Logo - Decision-Making
Decision-making and variables go together. A program needs to be able to change
course depending on the situation. Here, for example, is a framework for drawing a
spiral. It has a loop, a variation on the repetition shown earlier and the body of the loop
is for us to fill in.
to spiral
make "n 1
while [:n < 100] [
make "n :n + 5
fd :n rt 90
]
end
The above code shows several new features of the syntax of the MSW Logo. We set a
variable to a new value by keying-in ‘make’, then the variable's name is preceded by a
double quote " rather than a colon ‘:’ as shown below.
make "n 1
We use a variable, though, with a colon ‘:’ in front of its name.
while [:n < 100]
The code bracketed after the ‘while [condition]’ is executed, while the condition is true.
When it is no longer true, because (in this case) the value of ‘:n’ grows greater than 100,
the code following the bracket is executed.
Following screenshot shows the execution and output of the above code.
Now, we shall discuss the use of ‘if statements’, which have a code that will be
executed only when a given condition is true.
It also shows a built-in Logo that generates random numbers. The statement random
3 generates any number 0 or 1 or 2 arbitrarily in a random sequence. The procedure
then decides which way to go "at random". The generated random number will be kept
in ‘r’ and later depending upon the value of the variable ‘r’ one of the if-statements will
get executed, which will satisfy the condition. Thus if the −