0% found this document useful (0 votes)
11 views8 pages

Chapter 3 Lab

qazxc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views8 pages

Chapter 3 Lab

qazxc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Chapter 3: Logo Arithmetic & Control Structures

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.

In the above example, we have defined two variables first_name,


100_last_name_200 and initialized them with values Amal and Das using the following
statements −

 make “first_name “Amal


 make “100_last_name_200 “Das
Also, we printed these two variables with statements print :first_name and
print :100_last_name_200.
The following example shows how to define numeric variables −
Here, we have defined two numeric variables val1 and val2. We have also performed
addition and subtraction using them.

Logo - Arithmetic Operations


Logo provides the usual arithmetic operations of addition, subtraction, multiplication and
division, denoted by the symbols +, -, *, /. Each of these operations produces a result. If
you don't do something with the result, such as print it, Logo will show an error.
With the print command, the result of an arithmetic operation can be used and printed in
the command window. Examples given in the following screenshot demonstrate the
same.

Other useful commands are −


 sqrt − It takes one non-negative argument and returns its square root.
 power − It takes two arguments, call them ‘a’ and ‘b’, and generates a to the b
power.
 ln − It takes one argument and returns its natural logarithm.
 exp − It takes one argument and computes e to that power, e is the natural
number 2.718281828.
 log10 − It takes the logarithm to base 10 of its one argument.
Following screenshot shows an example of the above commands with their respective
output.

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 −

 Value of ‘r’ is 0, then [fd 20] will be executed.


 Value of ‘r’ is 1, then [rt 90 fd 20] will be executed.
 Value of ‘r’ is 2, then [lt 90 fd 20] will be executed.
Following screenshot shows the execution and output of the above discussion.

You might also like