The first program we will discuss is one to read in two numbers from the keyboard and compute the sum. It will compute the sum and put the sum on the screen.

You need to write "program blah;" where blah is some name on the first line.

You then write the word "var" followed by a list of names for each "box" you are going to use in your program. A box contains no more than one number. After the list of box names, you put a colon followed by the word "integer" or the word "real" Then you put a semicolon.

Then you write the word "begin" followed by a semicolon.

Now you have a bunch of statements each separated by a semicolon.

There are three kinds of statements introduced
a. the read statement
b. the assignment statement
c. the write statement

THE READ STATEMENT

Transfer a number from the keyboard to the box. "read (box_name)" copies one number from the keyboard to the box_name. Should there be something in the box already, it will be overwritten by the new value.

Once a number is read in, it is no longer available to be read in again. If you need hte value again, you need to keep it in a box or variable.

THE ASSIGNMENT STATEMENT

Then there is the "assignment statement." In it's simplest case, it will copy a number from from one box to another. When used in this manner, t is of the form:

variable1 := variable2

variable2 is copied to variable1. variable2 will remain unchanged. Whatever value might have been in variable1 is lost.

variable1 and variable2 are names specified in the "var" list at the second line of the program.

(Note the colon and the equal sign between the two variable names.)

The more sophisticated case allows us to perform arithmetic involving one or more variables prior to changing the variable on the left hand side.

This form of the assignment statement will evaluate the expression on the right hand side, getting a number. None of the boxes specified in the right hand side get changed--unless the name also appears in the left hand side. Then the computed value gets put in the box given by the left hand side. The right hand side is an ordinary algebraic expression and you can use parentheses. For real numbers, you can use the familiar "+", "-" "*" and "/" For integers, it is is "+", "-" "*" and "div" Since integer division truncates, the PASCAL compiler makes you write "div" to remind you of that. That is 3 div 2 is 1, not 1.5. You can also include numbers, referred to as constants, in your expressions. Multiplication and division of either flavor is done before addition or subtraction. This is the familiar "order of operations." Don't worry too much about all these rules, it all obeys "common sense."

THE WRITE STATEMENT

And finally, the write statement "write(boxname)" will copy the contents of the box to the screen where you are sitting.
Each successive value dumped by the write statement will appear at the end of the output. We will learn later how to output numbers to both the screen and into "files"

"writeln;" will cause the next bit of information to go onto the next line of the screen. Not very important in this example, but it will be useful in othe rprograms to keep things from getting jumbled up.

Line 1 defines the program name. "first_program" is the identifier. Pascal letts you put underscores in the name or for a box_name for that matter. We have defined three boxes in the program, a,b, and c.

Then we have the required begin.

Line 4 transfers a number from the keyboard to the box called a.
The second number typed in at the keyboard is put into the box called b in line 5.
Line 6, c:=a+b, computes the sum of the contents of the box called a and the contents of the box called b and puts that result in the box called c.
Line 7 will copy the contents of the box called c and puts it on display. In other words, it prints the result.

Line 8 sets up for the next output. And lastly, line 9 is the required end ofthe program. Notice that this ends with a period.

NOTE: The line numbers on the left margin are NOT to be typed!!!

They are there to enable me to refer to the lines in text and during lecture.



program first_program;
var a,b,c:integer;
begin
read(a);
read(b);
c:=a+b;
write(c);
writeln;
end.

Unit 1.2 Your First Program

PROG

read in two numbers, print out results

PED

to learn
basic format for a PASCAL program
how to read in a number
how to print out a number
how to do arithmetic with a number

CONCEPT

most manipulations are on boxes
boxes must be listed in "var" statement
use of
"real" - like a calculator
or
"integer" - ...-3,-2,-1,0,1,2,3

read(box_name) copies what you type into the box
write(box_name) copies what you type back to screen
box_name := expression involving box_names
computes the values of the expression. box_name indicated at left changes

SF

PROGRAM identifier;
var {put a list of box_name separated by commas here}:integer;
begin
put statements separated by ";"
it's nice to do one statement per line

end.