Notion of using a language for programming

Title:
Notion of using a language for programming
Date :
May 2007
Contributors: Srinath Perur
REF No:
3.29, 3.30
Std:
III
Reviewers:
Brief
Description:
Goal:
Prerequisites:
Duration:
Resources:
This lesson introduces the idea that a step-by-step procedure has to be
translated into precise directives for the computer to use. Simple
examples using the Logo programming language are used to
demonstrate.
So far the students have seen the computer perform several tasks.
Here the aim is to convey an intuition regarding how step-by-step
instructions must be provided to the computer by us in the form of a
program before it can perform those tasks.
Lesson on step-wise reasoning
Two classes
Detailed Description:
In the previous lesson we saw how a task can be performed after breaking it
down into several well-defined steps. In this lesson we see how a computer
manages to perform the tasks expected of it.
As you might have guessed, the computer requires very detailed and precise
instructions regarding how to go about performing a task. The instructions
required for the computer to perform a certain task (or a set of related tasks) is
called a program. The person who writes a program is called a programmer.
When we give instructions to other people, we use a language such as English or
Hindi. For giving instructions to a computer, we use a programming language.
Just like the languages we speak, a programming language consists of words
called keywords, and rules for putting them together so that the computer can
understand them.
Let us understand this better by making up a tiny programming language. It has
been found that complete instructions for many tasks we are interested in can be
given by combining only three types of statements. These are:
 do something
 if something has happened then do something
 repeat an instruction
In this case, our programming language has the following keywords: do, if . . .
then, repeat.
Recall that we wrote instructions in the previous lesson for making lemon juice.
Let us try to express those instructions as a program using the keywords we
have just defined:
1: do cut lemon;
2: do squeeze lemon into bowl of water;
3: if sugar is required then add sugar;
4: if salt is required then add salt;
5: repeat stirring until sugar/salt is dissolved;
Our lemon juice would be ready after this program. :o) (Also, observe that a
program works only if the instructions are carried out in the correct order!)
Applications such as TuxPaint and Writer that we use on the computer are all
programs. But unlike our program to make lemon juice, such programs can
contain many thousands of instructions. They are also written in more
sophisticated programming languages that have many more keywords.
Logo
Let us now explore a small part of an actual programming language called Logo.
On your Edubuntu system, you can click Applications->Education->KTurtle to get
the following window.
Ru
n
Turtl
e
Canva
s
The square in the right pane is called the canvas, and there is a turtle in the
center. In the left pane we can give Logo instructions to the turtle to move
forward or backward, or turn by a certain amount. The turtle has a pen, and will
leave a trail of its movements on the canvas. Thus we can use the turtle to trace
a figure on the canvas. While Logo can be used to write larger programs, here
we use only a few keywords that help us to control the movement of the turtle
and make it draw.
The Canvas
In the screenshot above, the canvas is the blank region in the right pane in which
the turtle draws. The canvas can be thought to be a fine invisible grid made of a
large number of squares. (A magnified10 x 10 grid is shown below for
illustration.) The canvas in KTurtle is a 300 x 150 grid by default, but it can be
changed as required.
The Turtle
The Turtle can be thought of as a cursor on the canvas. It obeys the commands
that we type into the left pane of the KTurtle window. When we tell the turtle to
move, we also must tell it how much to move. We express this in terms of the
number of squares the turtle has to move on the canvas grid. The turtle also
leaves behind a trail as it moves, so we can use it to draw on the canvas.
Now let us run a small, one-line program to move the turtle forward by 50 steps.
Note that since the canvas grid is very fine, this is not a large distance. To do
this:
 Open the KTurtle application as show in the screenshot
 Enter forward 50 into the left pane
 Click on the Run button indicated in the above screenshot
You should find that the turtle moves a short distance forward. You may want to
reduce the execution speed to see the movement more clearly. The following
screenshot shows the effect of running this simple program.
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.
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.
To draw a square, we repeated the same commands four times. Logo allows us
to express this more easily using the repeat command as follows.
reset
repeat 4 [
forward 50
turnright 90
]
Simple keywords
So far we have used the forward, turnright, reset and repeat keywords.
These, along with a few other simple keywords, are listed below for further
exploration.
reset
Positions the turtle at the centre of a blank canvas
turnright x
The turtle turns right by x degrees
turnleft x
The turtle turns left by x degrees
repeat n
<statements> are repeated n times
[<statements>]
penup
The turtle lifts its pen and leaves no trail as it moves
pendown
The turtle puts its pen down and leaves a trail as it moves
hide
The turtle becomes invisible
show
The turtle becomes visible
wait x
The turtle waits for x seconds before executing the next
instruction
Logo can also be used to accomplish more complex drawings, and other tasks
such as text processing and mathematical calculations. The KTurtle application
contains several example programs. Click on File->Open Examples in the
KTurtle menu and run these examples to see what the programs do. KTurtle
comes with an excellent manual with detailed instructions for learning Logo. This
can be accessed by hitting F1 or by clicking Help->KTurtle handbook on the
KTurtle window.
Lesson Plan:
Start by discussing with the class how the computer is able to do so many things.
Is it very intelligent?
Introduce the idea that computers are very good at obeying instructions. But the
instructions have to be very detailed and precise for the computer to follow them.
The instructions for doing a task are together called a program, and the person
who puts together the instructions is a programmer.
Just like we use English or Hindi to tell someone how to do a task, the computer
needs to be given instructions in a special language called a programming
language. Introduce the lemonade program as an example for keywords.
Introduce the KTurtle program -- a turtle who obeys commands and draws
wherever he goes. Then demonstrate in detail the Logo program to draw a
square. Then go on to show some more colourful Logo programs so that the
association is made between programs and the computer's ability to do things.
Note that the children are not expected to learn Logo programming at this stage.
It is enough if they grasp the idea of a turtle who obeys instructions and
understand the notion of the grid on which it moves. But it can be expected that
the children will pick up elementary commands such as forward, turnright
and turnleft from the demo session.
Worksheet
forward 2
turnright
forward 3
turnright
forward 1
1. Where will the turtle be after following these instructions? Trace the
path with a pencil and draw a turtle in the last square. One example is
shown below:
a.
forward 4
turnleft
forward 4
b.
turnright
turnright
forward 4
turnleft
forward 4
turnright
c.
forward 2
turnright
forward 2
turnright
forward 2
turnright
forward 2
2. Help the hungry turtle reach the carrots! Fill in the correct numbers
in the blanks. [Hint: first trace the path, then count the squares.]
forward ___
turnright
forward ___
3. The green turtle and the red turtle want to meet in the centre
square. Can you write simple instructions for each of them so that
they reach the square?
Green turtle
Red turtle