Lecture 5: Variables |
The instructions write and writeln are used for showing information on the screen: text, numbers, values of variables, etc.. They are very often used; nearly every program has somewhere a write or writeln instruction. As an example, consider the following program |
PROGRAM WritelnExample;
begin
writeln('Today is a very nice day');
end.
when we compile and execute the program the screen will show
Today is a very nice day
_
(note, the underscore character _ represents the position of the cursor).
The write and writeln instructions are not only used
for text, as in the example above, but also to show numbers, or other types
of information. Later we will learn how to control the format of the output
(the way the information is shown). For the moment, let's look at the brother
of writeln, namely write: The difference between writeln
and write is that writeln puts the cursor on a newline,
so that the next output (the time time we use write or writeln) will appear
below the first text. With write, the cursor stays directly after
the text:
PROGRAM WriteExample;
begin
write('Today is a very nice day');
end.
when we compile and execute the program the screen will show
Today is a very nice day_
When we want to print more things at the same time, we can do this with the same write instruction
PROGRAM WriteMultiple;
begin
write('Today is ', x, ' a very nice
day');
end.
which might show (when x is equal to 34)
Today is 34 a very nice day_
Compare the following programs:
program
PROGRAM WriteLnsExample; begin
output Today is a
|
program
PROGRAM WritesExample; begin
output Today is a very nice day_ |
program
PROGRAM WritesExample; begin
output Today is a very nice day_ |
program
PROGRAM WritesMulti; begin
output Today is a
|
Variables store values and information. They allow programs to perform
calculations and store the results for later use. Imagine a variable as
a box that can contain information. When we want to know this information,
we open the box and read the value. At the end, we put the information
back in the box and leave it there, until we need the information again.
For ease of identification, to avoid confusion, every box must have a name, an identifier (see aula 4). The type of the box defines the size of the box and the type of information that can be found in there. Variables come in many sizes and flavours. |
Variables can store numbers, names, texts, etc. In modern versions of
PASCAL there are many types of basic variables. The most important are
|
variable type |
|
|
|
boolean |
|
1 bit |
|
byte |
|
8 bit = 1 byte |
|
integer |
|
16 bit = 2 bytes |
|
word |
|
16 bit = 2 bytes |
|
longint |
|
32 bit = 4 bytes |
|
real |
|
48 bit = 6 bytes |
|
double |
|
64 bit = 8 bytes |
|
extended |
|
80 bit = 10 bytes |
|
char |
|
8 bit = 1 byte |
|
string |
|
256 bytes |
Note the convention of writing exponents in the scientific notation: 2.9E-39 means 2.9 x 10-39, 1.7E308 means 1.7 x 10308, etc.
I Boolean is used to store and manipulate
information of the type true-or-false. As we have seen in lecture
3, this is one bit of information.
III Real, double and extended are examples of variables that can store floating point numbers (for example 3.1415926535). These are used for things that are not countable, like the length of the car, the time elapsed between events, the height of a building, the square-root of 3, etc. The smallest is real, it occupies only 6 bytes, at the cost of smaller precision in our calculations. The best is extended, with 80 bits (10 bytes), the calculations will have very high precision, but the calculations will be slower and it occupies more space in memory. A good middle way is the double.
IV The last two types are used for storing text. Char is used for a single character, while string can store up to 255 characters.
Later we will learn how we can define our own type of variables, now
let's take a look at how we use them in PASCAL
PROGRAM VarExample;
VAR i: integer;
a: real;
b: word;
begin
writeln('Today is day ',i);
end.
If we want to define more variables of the same type, we can do that with several instructions or do it on a single line. Note that in any case, we don't have to write VAR again, although it is not prohibited:
PROGRAM VarsExample;
VAR i, j, k: integer;
a1, a2: real;
VAR b1: word;
b2: word;
b3: word;
begin
writeln('Today is day ',i);
end.
Today is day 23741
When a computer is switched on, the memory is normally filled with 0's,
but after a while, after many programs have been using the memory and left
there their garbage, the contents of a memory address is unpredictable.
To ensure that we are working with well defined values we always must assign
a value to each variable. In the next lecture we will learn how we can
do that with assignment instructions in the program. Here it suffices to
say that: "don't assume that your variables are set to 0 in the beginning".
Note: In many programming languages it is possible to assign a value
to a variable at the moment of declaration. Also in PASCAL it is possible
(via variable constants), but this will add to the confusion and we better
avoid doing that.