CSE 111, Fall 2000

Great Ideas in Computer Science

Lecture Notes #13

PROGRAMMING IN PASCAL:
DECISION TREES

(3.  Programming in Pascal, continued)

b)    Sample Pascal program:


        program printdata;
        begin
          writeln('This is a');
          writeln('test program')
        end.

    NOTES about the above program:

    1-    It could have all been written on one line.
    2-    The spacing & indentation are for human
            readability
                *    and are not enforced by the
                      compiler, as they are in Karel
    3-    The order of the instructions IS important

c)    Programs are written in a programming
        language, which has a grammar (syntax);

        *    The compiler checks the syntax before
             the computer executes the program

        *    Actually, the compiler checks the syntax
            before translating the program into the
            machine language of the computer

            >    That's what the "pc" program does

        *    If there's an error (bug),
                the program won't compile
                & you'll get error messages.

        e.g.) Here's a Pascal program with 2 bugs:

                program test;
                begin
                    witeln('hello')
                    writeln('goodby')
                end.

            When this is compiled with "pc" on Unix,
            you'll get these error messages:

Mon Oct  2 10:44:50 2000  test.p:
           3          witeln('hello')
E 18420---------------^---  Undefined procedure
           4          writeln('goodby')
e 18480---------------^---  Inserted ';'
In program test:
E 18240 witeln undefined on line 3

The first line gives the date & time of compilation
and the name of the file that was compiled
(in this case, "test.p")

Then it says that on line 3, which contained
"witeln('hello')", there was a serious Error, with
a capital E, namely, that there was an undefined
procedure.  Serious errors prevent the program
from running.  In this case, the error was simply
that I misspelled "writeln", and the compiler
thought I was asking it to perform a procedure
that I hadn't defined.

Next, it says that on line 4, which contained
"writeln('goodby')", there was a non-serious
error, with a lower-case e, namely, that it decided
I needed a semicolon before that line (which I did),
and it kindly inserted it for me in the machine-
language version of the program (it did NOT modify
my "test.p" file).

Final note:  Just as there are different dialects of
English (e.g., New York City English, in which Coke
and Pepsi are "sodas", and Buffalo English, in which
they are "pop"), there are also different dialects
of Pascal.  The Biermann text uses Turbo Pascal;
we are using Unix Pascal.  I will alert you to the
differences when we come to them.

e)    Storing things in memory:
        How to input information to the computer

*    a "computer" (in ordinary language) is really
      2 things:  a Central Processing Unit (CPU)
      & memory.

*    The CPU is a computer chip, e.g., a Pentium III
     or Celoron processor.

*    Think of a memory location as a box into which
    information can be put.

*    Each memory location can be given a name,
    by you, in your program.

e.g.)    Let's set up a memory location named 'word'
            that will contain a "string" of 20 characters:
 
          The "box" in memory will contain 0s and 1s
          (switch settings), the number of which will
            depend on the computer, but you don't
            have to know anything about it; the Pascal
            compiler ("pc") worries about that.

        To do this in our Pascal, just say:

    var word : varying [20] of char;

    This is called a "variable declaration".

    "var" is a reserved word
    "word" is the name we give to the memory location
    "varying", "of" and "char" are also reserved words
    "char" indicates that the contents of the memory
        location will be characters (letters, numerals,
        symbols).
    & the name (in our example, "word") you give
        to the memory location is called a "variable"

e.g.)    program InAndOut;
          var word : varying [20] of char;
          begin
            writeln('Enter a word:');
            readln(word);
            writeln('The word you entered was:');
            writeln(word)
          end.

This is a program, named "InAndOut".
The first thing that it does is create a memory
    location, named "word", that can contain
    up to, but not more than, 20 characters
Then it outputs the message "Enter a word:"
    & it puts the cursor on the next line
    (that's what the "ln" in "writeln" means);
    Note that what it outputs is put in single quotes
Then it waits till the user types a word.
It then puts that word into the memory location
    called "word" & puts the cursor on the next line
    (that's what the "ln" in "readln" does);
    Note that "word" in the instruction "readln(word)"
    does NOT have single quotes.
Next, it outputs the message "The word you entered
    was:" & moves the cursor to the next line.
Finally, it takes the information in the memory
    location named "word" and outputs it.
 


Copyright © 2000 by William J. Rapaport (rapaport@cse.buffalo.edu)

file: 111F00/lecturenotes13.05oc00.html