Lecture 5: Variables


Write and Writeln

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
  writeln('Today is a ');
  writeln('very nice day');
end.

    output

Today is a 
very nice day
_

     program

PROGRAM WritesExample;

begin
  write('Today is a ');
  write('very nice day');
end.

    output

Today is a very nice day_

    program

PROGRAM WritesExample;

begin
  writeln('Today is a ');
  writeln;
  write('very nice day');
end.

    output

Today is a 

very nice day_

    program

PROGRAM WritesMulti;

begin
  writeln('Today ','is',' a ');
  write('very nice day');
end.

    output

Today is a 
very nice day_


Types of Variables

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
 

 group
 variable type
 range
 size it occupies in memory
I
 boolean
 TRUE / FALSE 
 1 bit
II
 byte
 0.. 255
 8 bit = 1 byte
II
 integer
 -32768 .. 32767
 16 bit = 2 bytes
II
 word
 0 .. 65535
 16 bit = 2 bytes
II
 longint
 -2147483648 .. 2147483647 
 32 bit = 4 bytes
III
 real
 -2.9E-39 .. 1.7E38
 48 bit = 6 bytes
III
 double
 -5.0E-324 .. 1.7E308
 64 bit = 8 bytes
III
 extended
 -3.4E-4932 .. 1.1E4932
 80 bit = 10 bytes
IV
 char
 #0 .. #255
 8 bit = 1 byte
IV
 string
 text
 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.
 
 

II Byte, integer, word, and longint all store values of complete numbers. This is used for things that can be counted; number of people in the room, number of doors of a car, number of phonecalls somebody made, ano lectivo, day of the month, etc. Byte and word only store positive numbers, while integer and longint can store both positive and negative numbers, at the cost of limited maximum value. The byte occupies the least memory, only 8 bits, but the range of values it can take is therefore very limited, only 0 .. 255. If we need to store larger positive complete numbers, we have to use word. If we want to store large numbers and want positive and negative numbers we use longint.
Note: since a byte has 8 bits, it can store 28 = 256 different numbers: 0 .. 255. The same calculation we can make for the 16-bit units word and integer: 216 = 65536, numbers from 0 .. 65535 for word, and -32768 .. 32767 for integer. Remember the calculations of lecture 3.

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


Variables: Declaration with VAR

In most modern compiled languages, all variables that we will use have to be defined first. This is called declaration. To be more precise, declaration means reserving space in memory and associating a name to it, so that later we can use the name instead of the memory address when we want to retrieve the information.
In PASCAL we declare variables with the instruction VAR, followed by the variable(s) with type that we want to use. The place to do that is before the begin of the program, but after the declaration of the name of the program. For example:

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.


Variables: Initialization

The VAR instruction does not assign a value to the variable, it only reserves space for it in memory! In the last example program above, the output might have been

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.


Quick test:

To test your knowledge of what you have learned in this lesson, click here for an on-line test. Note that this NOT the form the final test takes!

Peter Stallinga. Universidade do Algarve, 23 fevereiro 2002