which is just our old "conversation.p"
program
rewritten using the concatenation
operator.
e.g.) Here's yet another example, from Biermann,
Ch. 2, p. 48, #2:
I/P: noun
O/P: plural
of noun
program plurals;
var userstring, pluralstring
: varying [21] of char;
begin
writeln('Enter
a noun:');
readln(userstring);
pluralstring := userstring + 's';
writeln('The
plural of your noun is:');
writeln(pluralstring)
end.
Try it! Note that I've
divided the body of the
program into 3 parts (indicated
by spacing):
an input section, a computation
section (where
the process of creating
the plural is done, using
concatenation), and an output
section.
Although this program compiles,
and
although it does correctly
form the plural
of certain English nouns,
it doesn't really
work as advertised:
What does it output as the
plural of "mouse"
or of "sheep"? Hopefully,
we'll have time later
to return to this problem
and write a slightly
better program.
a) Here's a new type, to add to our collection
that, so far, includes these
types:
char
- any printable character from
the ASCII set
varying [n] of char (where n is an integer)
- any string of n such characters
Here's the new type:
integer - any integer
By the way, the integers
are the following
numbers:
..., -3, -2, -1, 0, 1, 2, 3, ...
So, e.g., -796, -2, 0, 5,
and 3987 are all
integers, but 2.5, 3 1/2,
pi, etc., are not.
Well, the "integer" type
does not really
contain "any" integer:
Computers are
finite machines, so there's
a largest and a
smallest integer that we
can deal with. To
find out what they are,
here's a small Pascal
program that will show you
what they are on
our system.
e.g.) Here's a small Pascal program that shows you
how we can use the "integer" type:
program add;
var x, y, z : integer;
begin
x
:= 2;
y := 3;
z := x + y;
{note that this use of "+" is arithmetic}
{addition, NOT string concatenation! }
writeln('x
= ', x);
writeln('y = ', y);
writeln('x + y = ', z)
end.
What does this program do?
1. It sets up 3 memory locations, called "x", "y",
and "z", each of which can contain an integer.
2. It puts the integer 2 in location "x"
3. It puts the integer 3 in location "y"
4. It gets the value stored in "x" (i.e., 2)
and the value stored in "y" (i.e., 3),
and then it adds them; finally, it puts
the sum in location "z"
5. It outputs the message:
x = <value stored in x>
6. It outputs the message:
y = <value stored in y>
7. It outputs the message:
x + y = <value stored in z>
Try it!!
What happens if we write:
x := '1'
instead of
x := 1
? Try it!!