Learning Commands:

Program, Readln, Writeln, Write, assignment

week1ex1

All of these programs do the same thing:
They ask the user for two numbers and then report the sum.
They differ in the elegance with which the output is displayed .

In the simplest version the output is two lines:
the line requesting the numbers and the line reporting the total.
To test your understanding change either or both of the writeln statements.
For example as a first try you might making the first letter of each line a capital letter.

Hint: when you type the two numbers put a comma between them


week1ex2

In this program the second line reports not only the total
but also the two input numbers .
When you run this you will notice that there is a lot of space before each number.
This is what should happen and the next example will show you how to do this.

You might make this look a bit better
by breaking the last line into three lines such as :

The sum of #
Added to #
Gives a total of #
How about changing the first output line to tell the poor user
to put a comma between the numbers.


week1ex3

In this version the starting line now gives the the user more information.
Also the user is asked for only one number at a time. That way he doesn't
have to know how the two numbers should be separated.
Even better -- the line printing the numbers now has more reasonable spacing.
But the spacing is still not right. Can you fix it?


week1ex4

Here we get the spacing of the answer line right!
The programmer must work to get the spacing right

We also introduce a new type of variable -- the string
Strings hold characters while integers hold only numbers
You can put numbers in a string variable because they are a
kind of character. But you can't do arithmetic on strings even if
the string is all numbers.


week1ex5

This version illustrates the use of both WRITE and WRITELN
(WRITELN puts a carraige return at the end of what it writes)

As a beginning programmer you won't have to worry about using WRITE for
some time (over half the course.) By then you'll see why we would want such
a thing. For now you can read this example and then forget it.


Rules for writing Pascal statements

General Index to Sample Programs

[ Home ][ Vita ][ Copyright  ][ Copyright  ]