60
Introduction to Programming in Turing
Figure 2.19 Print Dialog Box on the Macintosh
2.10 Example Turing Programs
Even though you have not yet studied how to create your own
Turing programs, you can learn about the Turing environment by
typing programs in and running them.
When you start the Turing environment it automatically
provides a blank Editor window in which you can begin entering
your program. If there is no blank Editor window, you simply
select the New command from the File menu.
Here is a program for you to type in and store on the disk.
When you type this program in, do not indent it. Instead, wait until
you have typed in the entire program and select the Indent
command. Notice how the automatic indenting structures the
parts of the program.
% The "TimesTable" program
% Outputs a multiplication table
var number : int
put "Choose a number between 1 and 12 " ..
get number
put "Here is the multiplication table for ", number
for i : 1 .. 12
put i : 2, " times ", number, " is ", i * number : 2
Chapter 2 : The Turing Environment
61
end for
Once you have entered and indented this program, select the
Run command from the Run menu. The screen clears when the
program begins execution. When the prompt line
Choose a number between 1 and 12
appears on the screen, type a number between 1 and 12
inclusive. When you finish typing, press Enter so the computer
knows you have finished. As soon as you press Enter, the
multiplication table appears for the number you have chosen. The
number you type is shown here in boldface.
Here is a sample Execution window:
Choose a number between 1 and 12 6
Here is the multiplication table for 6
1 times 6 is 6
2 times 6 is 12
3 times 6 is 18
4 times 6 is 24
5 times 6 is 30
6 times 6 is 36
7 times 6 is 42
8 times 6 is 48
9 times 6 is 56
10 times 6 is 60
11 times 6 is 66
12 times 6 is 72
Suppose you wanted to have a table showing your number
multiplied by the values 1 to 20 rather than 1 to 12. Can you
guess what change in the program might do this? Try changing
the program and then run it again. Store the changed program
under a new file name, say TimesTable20.t.
Try changing the program by substituting a / for the * in the
program. This produces a division table instead of a multiplication
table. Can you fix the rest of the program to suit this change?
Store this program as Divide.t on the disk.
62
Introduction to Programming in Turing
Here is a slightly longer program. When you have finished
typing it in, give the Run command and play the guessing game.
After you have played the game try reading the program to see if
you can understand some of it.
% The "GuessNumber" program
% Chooses a number at random between 1 and 99
% and allows you to guess it
var hidden, guess : int
var reply : string (1)
put "See if you can guess the hidden number"
put "It is between 1 and 99 inclusive"
loop
var count : int := 0
put "Do you want to play? Answer y or n " ..
get reply
exit when reply = "n"
% Choose a random number between 1 and 99
randint (hidden, 1, 99)
loop
put "Enter your guess (any number between 1 and 99) " ..
get guess
count := count + 1
if guess < hidden then
put "You are low"
elsif guess > hidden then
put "You are high"
else
put "You got it in ", count, " guesses"
exit
end if
end loop
end loop
Save the program on the disk as GuessNumber.t
Chapter 2 : The Turing Environment
63
2.11 Exercises
1. The program window can be used to enter any kind of data.
Clear the program window, then enter a short letter to your
teacher telling her or him how exciting it is to be using the
computer as a simple word processor. Print the letter if you
have a printer. Store the letter on the disk under the file name
Teacher. Check the directory to see that it is there.
2. Change the letter you wrote for question 1 so that an extra
paragraph is added about how simple it is to edit text on a
computer. Arrange to address this same letter to a friend as
well as to your teacher. Print both new letters if you have a
printer. Store them as files called Teacher2 and Friend. Check
the directory to see that all three files are there.
3. Here is a Turing program to type into the program window and
run.
% The "Seesaw" program
% Makes saw tooth patterns
loop
put "How many teeth do you want? (1–12) "
var count : int
get count
put repeat ("* ", count )
put repeat (" * *", count )
put repeat (" * * ", count )
put repeat (" * ", count )
end loop
Try running the program. If you get tired of making saw tooth
patterns you can stop the execution of the program by selecting
Stop from the Run menu.
© Copyright 2026 Paperzz