Introduction to Programming


*Note*    In 2007, these lessons were significantly modified.    *Note*
This is a link to the new materials.
This is a link to the new pseudocode lesson.


For getting the turtle to draw simple things, like a square, you can just enter instructions in the CommandCenter and most of the time your program just works.  It does what you wanted it to, what you thought it would do.  However, when you write any non-trivial program, this will not be the case.  The more instructions you include in your programs, the more you need to keep in your head about what you're doing and the more typing you need to do.  This means more chances for making mistakes.

In this lesson you will learn how to write computer programs in stages.

The first stage will be to think about what you need to do.

In the next stage you will write what you want the program to do in pseudocode.

    pseudo adj. 1: being apparently rather than actually
           as stated.  ( Webster's New Collegiate Dictionary )

    Pseudocode (derived from pseudo and code) is a description
    of a computer programming algorithm that uses the structural
    conventions of programming languages, but omits detailed
    subroutines or language-specific syntax.  ( WikipediA )
A third stage is the conversion of the pseudocode into properly formed instructions that are available in the programming language you are using, Logo in our case.

How To Write Non-Trivial Programs
That Do What You Want

When you did the Exercises at the end of the last lesson, my guess is that you didn't type in programs that worked perfectly the first time.  If you did, congratulations... you are great at visualization and orderly thought.  The good news is that anyone can learn to write correct programs - an experienced programmer would have no problem writing "correct" programs for these exercises. 


As you are about to see, an experienced programmer would think about how to solve the problem before she would start typing in instructions.  An experienced programmer would break the problem into pieces that each are simple to do by themselves.  Then, put all the pieces together to solve the problem.

Let's play "experienced programmer" with the first exercise.

Understanding the Problem

What you should do first is think about what you need to do.  Get out some paper and a pencil to write down your thoughts.  Look at the diagram carefully.  How can you break it up into multiple, simpler to do parts.

You need to get the turtle to:

This completes the "Understanding the Problem" phase. 

The next phase is "Devising a Plan" - how do you get the turtle to draw the things you've identified.

Devising a Plan

So you now have some notes on your piece of paper that describe ways that lead to a solution.  The next step is to decide on an approach and put together a process which gets the turtle to draw the figure.

Let's follow our experienced programmer's thoughts through the rest of a solution.  She has chosen to draw a square (a special kind of rectangle) and subdivide it. 

The first step: figure out how many turtle steps each side of the square needs to be.  Since it will be split in half and into thirds, she needs a length that is a multiple of 2 and 3.  She likes nice round numbers so she makes each side of the square 120 turtle steps (120/2=60; 120/3=40).  As you can see, it's nice to know a little bit of math when you're programming.

With this decided, she first writes her program down on paper in pseudocode.

   1. draw a square
   2. draw the vertical line that splits the square in half
   3. draw the top horizontal line spliting the right half
   4. draw the bottom horizontal line spliting the right half
   5. make the turtle invisible
Pseudocode is a term for describing something in your native language.  Her pseudocode is English descriptions of what she wants her program to do.  Once this is complete and she is convinced that her plan should produce what she wants, it's time for the next phase: converting the pseudocode into Logo instructions.

Carrying Out the Plan

Time to convert the plan into a program that can be executed on a computer.  Since she is programming in Logo, our expert programmer will convert the pseudocode into instructions available in this computer language.  Table 3.1 shows both the pseudocode and the results of its conversion into Logo.

(Written on Paper)
Logo Instructions
(Typed on Computer)
 draw a square  fd 120 rt 90 fd 120 rt 90 fd 120 rt 90 fd 120 
 draw the vertical line that splits the square in half   bk 60 rt 90 fd 120 
 draw the top horizontal line spliting the right half   bk 40 rt 90 fd 60 bk 60 lt 90 
 draw the bottom horizontal line spliting the right half   bk 40 rt 90 fd 60 
 make the turtle invisible   ht 
Table 3.1

Our experienced programmer brings up the TG Applet in a browser on her computer.  TG understands the Logo programming language; it contains a Logo interpreter.

She reads her pseudocode notes (converting them to Logo instructions in her head) and types them into the CommandCenter.  As each line is [Enter]-ed she watches the turtle do exactly what's expected.

Using TG's Editor Instead of Paper

It is possible to switch from pencil and paper to the TG programming environment applet for the "Devising a Plan" and "Carrying Out the Plan" stages.

I was taught to program in this manner 36 years ago.  But, my interaction with a computer consisted of getting a little bit of time on a computer I shared with hundreds of others.  So, use of paper and a pencil for writing programs was important.  But, now everyone has their own computer...  Let's take advantage of this and eliminate the use of paper and pencil.

You will do this via TG's editor

The editor is available via the Window->Editor->Open menu item.  In the application, the menu system is readily available via the standard, pull-down stripe across the top.  To access the menu system in the applet, position the mouse in the applet and hold down the right mouse button.  Position the mouse over the "Window" menu item to bring up its submenu, choose the "Editor" submenu, and finally choose its "Open" option.  Figure 3.1 shows this.

Figure 3.1

Figure 3.2 shows the result, an editor subwindow is now below the CommandCenter, at the bottom of the applet.

Figure 3.2

At this point, the height of the subwindows can be adjusted by positioning the mouse over one of the name stripes, holding down the left mouse button, and dragging the name stripe up or down.  Since we are going to be using the editor, make it a bit taller; first drag the CommandCenter name stripe up, then drag the Editor name stripe up.

So, for "Devising a Plan" we need to enter the pseudocode representation of the program.  Well, this is a problem.  Logo doesn't understand pseudocode and it and will complain if we type it in.  We solve this problem by entering the pseudocode as comments.  Comments in Logo start with the semicolon (";") character and go through the end of the line.  Comments are intended to be read by humans and are ignored by Logo.

Figure 3.3 shows the TG applet after the pseudocode from the previous exercise has been entered into the editor.

Figure 3.3

Now we are ready for the "Carrying Out the Plan" stage.  Under each comment, we convert the pseudocode into the appropriate Logo instructions.

Figure 3.4 shows the TG applet after the Logo instructions have been added.

Figure 3.4

So, unlike when we entered Logo instructions into the CommandCenter, nothing has been happening while we enter stuff in the Editor.  How to we get the Logo interpreter to do what's in the Editor?

You use the Interpret -> Editor Contents menu item.  Figure 3.5 shows me doing this - after I resized the Editor and CommandCenter subwindows near their minimum sizes.

Figure 3.5

And finally, Figure 3.6 show the results, our set of boxes drawn exactly as expected...

Figure 3.6

Exercise: Draw a House

Try this approach to programming just described.  Write a program that draws a simple house.  Go through all of the steps I've talked about:

  1. Understanding the Problem,
  2. Devising a Plan, and
  3. Carrying out the Plan.

 Although it's not the
 most attractive house,
 it is easy to draw
 with the turtle.

 Make the front of the
 house a square with
 each side 100 turtle

 Make the roof an
 equilateral triangle,
 with each side 100
 turtle steps.

 Make the door 50
 turtle steps high
 and 25 turtle steps

 Make the window a
 square, 25 turtle
 steps on each side.
Figure 3.1

First, write pseudocode for how you will go about drawing the house and then convert this to Logo instructions.  If you prefer paper and pencil, do it this way.  If you prefer to type stuff into TG's editor, here's the applet.

alt="Your browser understands the <APPLET> tag but isn't running the applet, for some reason." Your browser is completely ignoring the <APPLET> tag!
TG Programming Environment Applet

If this applet is broken and you are using Chrome, click here.


One BIG thing we learned in this lesson was how to write a program in phases:

    1. Understanding the Problem
    2. Devising a Plan
    3. Carrying out the Plan
These are the first three of George Polya's four steps to solving mathematical problems.  Dr. Polya wrote a very good book called How to Solve It which explains his methodology. 

I've saved the fourth step for this summary, it's:

    4. Looking Back
How appropriate.  Think about how you went about drawing the house.  What was the hardest part?  If it didn't work the first time, what was the mistake, or mistakes?  Can you think of any way to do your next project so that you don't make the same mistake(s) again?

Back to Table of Contents
Back to previous Lesson ( Introduction to Commands )
On to next Lesson ( Defining Your Own Commands )