1.1 The First Program

Here is the code for creating the classic first program:

put "Hello world." 

It will produce the following output:

Hello world.

This is much shorter than most programming languages. To print things in a Turing program you use the put statement. Anything that appears in double quotes following put will be printed. Here is a program that prints several lines of output:

put "                       W  L  T OT"
put "Toronto Maple Leafs   40 20  8  3"
put "Ottawa Senators       36 24  9  2"
put "Montreal Canadiens    30 28 11  5" 

It will produce the following output:

                       W  L  T OT
Toronto Maple Leafs   40 20  8  3
Ottawa Senators       36 24  9  2
Montreal Canadiens    30 28 11  5

Notice that spaces inside quotes are printed. Spaces in other places are ignored by the computer (although it is harder for a person to ignore). The following program produces the same output as the previous program:

put                   "                       W  L  T OT"   put
                            "Toronto Maple Leafs   40 20  8  3"
                            
    put         "Ottawa Senators       36 24  9  2"
 put"Montreal Canadiens    30 28 11  5" 

You should follow the indentation and spacing style of programs presented in this text.

After a put statement the computer will go by default to the next line. That is why the teams get printed out on their own lines. It is possible to tell the computer not to go to the next line. You do that by putting .. at the end of a put statement. Here is an example of another way of doing the "Hello World" program:

% A longer version of "Hello world"
put "Hello" ..
put " " ..
put "world." 

You get the exact same output as the first version. The first line of the program is called a comment. A comment starts with the percent sign. The computer ignores this. You can use comments to help explain how your programs work. For assignments you will also use comments to put information into your program such as your name and the due date of the assignment.

If you want a blank line you would use put with an empty ("") string after it. Here is an example:

put "    This"
put ""
put "     is"
put ""
put "double spaced" 

It would produce this output:

    This

     is
     
double spaced

Double quotes are used to indicate the beginning and ending of a string constant in a put statement. In order to print a double quote you need to put a back slash in front of the quote. For example the program:

put "The student said, \"NTCI is a great place\"." 

would produce:

The student said, "NTCI is a great place".

Exercise 1.1

  1. What does this program print?
    put "The Cat " ..
    put "In The Hat" ..
    put ""
    put ""
    put "        by"
    put ""
    put"   Doctor Suess" 
    1. Rewrite the following program using the suggested indentation style.

      put "This is a " ..  put "test"  .. put "!!!" 
    2. What does the program in part a) print?
  2. Write a program to print your name and address as they would appear in a letter.
  3. Write a program to print your name in giant letters as shown in the following example.
         A     L       EEEEE   X     X
        A A    L       E        X   X
       A   A   L       E         X X
       AAAAA   L       EEE        X
       A   A   L       E         X X
       A   A   L       E        X   X
       A   A   LLLLL   EEEEE   X     X
  4. Write a program that would produce the following output using 5 put statements.

    The coach said, "We have 'some' things to work on". He paused,
    then said "Just our offence and our defence!".