Lecture 6: Assignment and Constants


Formatted Write and Writeln

Using write and writeln without specifying the format will show the values in standard form, which is the scientific notation for reals and 'as many places as is needed' for integers (inteiros):

  PROGRAM WritelnUnformatted;

  Var r: real;
      i: integer;

  begin
    writeln('Real r = ', r);
    writeln('Integer i = ', i, '!!');
  end.

output:

  Real r = 3.00000E-2
  Integer i = 1!!

To change the way the values are presented, we can specify the width of the text, and, for variables of floating point type (real, double, extended), the number of cases after the floating point. Note the following example:

  PROGRAM WritelnFormatted;

  Var r: real;
      i: integer;

  begin
    writeln('Real r = ', r:8:6);
    writeln('Integer i = ', i:5, '!!');
  end.

output:

  Real r = 0.030000
  Integer i =     1!!

The real r is shown with a total of 8 places reserved, and 6 digits after the floating point. The integer i is written with 5 places of text. Since in this case only one digit was enough, the rest, 4 places, is filled with empty spaces.


Assignment

In the previous lecture (aula 5) we have seen how we can define variables. Now we will take a look at how the value of a variable can be assigned a value

Giving a new value to a variable is called assignment. In PASCAL this is done with the operator
 
 







On the left side op this operator we put the variable, on the right side we put the new value or expression that will produce a value of the same type as the variable. Examples:

  a := 3.0;
  b := 3.0*a;
  c := Cos(t);

wrong (assuming a is of type real):

  1.0 := a;
  a := TRUE;
  a := 1;

In reality, for PASCAL the last example was correct. PASCAL knows what we want and helps us by converting the integer 1 to the real 1.0. In other languages (C, FORTRAN) this is not so. In any case, it is bad practice to mix reals with integers and we'd better write:  a := 1.0;

The symbol := is pronounced as 'will be', or 'becomes'. This to distinguish it from the normal mathematical symbol =. At this stage, it is interesting to make a comparison between the mathematical symbol = and the assigment symbol in programming languages (:= in PASCAL). As an example take the following mathematical equation
  a = a + 1
This, as we all know, does not have a solution for a. (In comparison, another example: a = a2 - 2, has a solution, namely a = 2).
In programming languages, however, we should read the equation differently:
  a := a + 1;
means
  (the new value of a)     (will be)    (the old value of a)     (plus 1)
Or, in other words: first the value of a is loaded from memory, then 1 is added to it, and finally the result is put back in memory. This is fundamentally different than the mathematical equation. Exactly for this reason, in PASCAL the symbol := was invented for assignment. To avoid confusion. In most other modern languages the symbol = is used, which is confusing, especially for the beginning programmer. That is why the student is advised to pronounce the assigment symbol as 'becomes' or 'will be' instead of 'is' or 'equals'.
 
 

 To summarize, the := symbol is 
   NOT part of a comparison ("a is equal to b?") 
   NOT part of an equation ("a2 = 2a -1")
   it IS an assignment ("a becomes b")

Later, we will learn how to make comparisons (in the lecture on "if .. then" and "boolean algebra"), for which we will use the = symbol.


Constants

Constants differ from variables in that they cannot change their value during the running of the program. Once assigned a value, it cannot be changed. The advantage of this is that we can define a value at one place in the program and from that moment on use it wherever we need it. Consider the example of p. It is much easier to once define this value than to every time have to type 3.1415926535 when we need it's value. The program becomes more readable in this way.
The definition of constants we do at the same place as the variables, but with the word CONST, so for example:

  PROGRAM ConstExample;

  VAR angle: real;
      tan: real;
  CONST PI = 3.1415926;

  begin
    angle := 45.0;
    angle := PI*angle/180.0;
    tan := Sin(angle)/Cos(angle);
  end.

Note the way the constant is written, namely in UPPERCASE. Remember that PASCAL is not case sensitive and we can mix uppercase and lowercase whenever we want. Still, the use of uppercase for constants is a useful convention which the student is advised to follow. Any programmer reading our program can rapidly see that he is dealing with a consatnt instead of a variable.

Interesting technical detail: To be more precise, for constants no space in memory is reserved like it is for variables. Instead, the compiler, everytime it encounters the name of our constant in our text, will substitute it's value. We do not have to worry about this detail, though. For all good purposes, we can assume that a constant is a variable that cannot change value.

Consider the following example:

  PROGRAM InterestRateExample;

  VAR money: real;
  CONST TAXA = 4.3;

  begin
    money := 99.0;
    money := money*(1.0+TAXA/100.0);
  end.
In case the interest rates change, we only have to change our program in one place (probably somehwere in the beginning). This avoids errors and inconsistencies in the changed program. If we forget to change it in one place the program will produce erroneous output, and now imagine a program with thousands of lines of PASCAL code. It is easy to miss one instance of our interest rate.


Example

The following shows an example of a program using assignment statements and the use of variables and constants. The right side shows the value of the variables after each line
 
value of a
after executing the line
value of b
after executing the line
PROGRAM Assign;

VAR a: real;
    b: real;
CONST C = 4.3;

begin
  a := 1.0;
  b := C;
  a := a + b + C;
end.


 
 
 
 

undefined
1.0
1.0
9.6


 
 
 
 

undefined
undefined
4.3
4.3


Quick test:

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

Peter Stallinga. Universidade do Algarve, 23 fevereiro 2002