Lecture 7: Math


Read and ReadLn

In the previous lectures (see aula 5 and aula 6) we have learned how to show the results of our calculations on the screen. With write and writeln we can make our program have output. In many cases, we would also like our program to have input. The user enters his name, or enters numbers that our program has to process. Or even more simple, we want that the users can control the program. For example, we want that the user can stop the program with a simple stroke of the escape key.
 
 
PASCAL: With Read and ReadLn we can get information from the keyboard. They can read values and characters from the keyboard and directly store them into specified variables. The general form the instructions take is

Read(variable_list);

ReadLn(variable_list);

Here variable_list is a list of variables to be read, seperated by commas, for example

Read(a, b, i);

Note that these variables do not have to be of the same type. In the above example they could be of type real, boolean and integer respectively. As an example

  PROGRAM ReadExample;

  VAR n1, n2: integer;

  begin
    WriteLn('Please enter two numbers separated by a space');
    Read(n1);
    Read(n2);
    WriteLn('n1 = ', n1 ,'    n2 = ', n2);
  end.

When run, the program will display the message 
  Please, enter two numbers separated by a space
The user is expected to type the two numbers
  128 31<return>
(note the conventions we will use in this text: things the user will enter will appear in green italics, and <return> signifies pressing the return key).
after which the program shows
  n1 = 128  n2 = 31

The difference between Read and ReadLn is that the ReadLn statement discards all other values on the same line, while Read doesn't. In the above example, if we had replaced the Read instructions with ReadLn instructions, the program would have assigned 128 to n1, then discard the rest of the text the user typed and consequently wait for more input to assign to n2.
 


Math

After learning how to assign values to variables in lecture 6, we can now take a look how to perform calculations with our programs. The most basic operations are adding, subtracting dividing and multiplying:
 
 operator 
 operation 
+
addition
-
subtraction
*
 multiplication 
/
division

These four operators, when used for calulations, need two operands. In PASCAL one is placed on the left side and one on the right side. As examples:
 
 correct
 wrong
3 * a
* a
a + b
3 a +

These expressions will result in values that can be assigned to variables as we have seen in lecture 6. As an example:

c := 3 * a;

Note again, on the left side of the assignment symbol := we have a variable and on the left side we put our expression resulting in a new value for the variable.


Integer Math


The operators shown in the previous section are used for floating point calculations. For integer calculations (used for types like byte, word, integer, longint, etc), the division operator is not used. Instead, we have two new operators that also have an equivalent in modern mathematics. Imagine the calculation of, for example, 7/3. As we have learned in primary school, this is equal to 2 with a remainder of 1 to be divided by 3:
 
 

7
   
1
------
   = 
2  + 
------
3
   
3

In PASCAL exist two operators Div and Mod that reproduce these results. Example:
 

 expression 
 result 
 7 Div 3
2
7 Mod 3
1

These replace the floating point operator /. The other three operators (*, +, -) are the same for integer numbers.


Priority

In case there is more than one operator in an expression, the normal rules of mathematics apply as to which one is evaluated first. The multiplication and division operators have higher precedence. So, when we write
   a := 1 + 3 * 2;
The result will be 7.
If we want to change the order at which the operators in the expression are evaluated, we can always place parenthesis ( and ). So, for example
  a := (1 + 3) * 2;
will result in 8. Putting parenthesis never hurts!
  a := (1 + 3) - (4 + 5);

Examples

PROGRAM SimpleCalculations;

VAR x, y, sum, diff, divis: real;

begin
  WriteLn('Give the value of the first variable x:');
  ReadLn(x);
  Writeln('Give the value of the second variable y:');
  ReadLn(y);
  sum := x + y;
  Writeln('The sum of ', x:0:4, ' and ', y:0:4, ' is ', sum:0:4);
  diff := x - y;
  Writeln('The difference between ', x:0:4, ' and ', y:0:4, ' is ', diff:0:4);
  divis := x / y;
  Writeln(x:0:4, ' divided by ', y:0:4, ' is ', divis:0:4);
end.

will produce, when running:

Give the value of the first variable x:
3.4
Give the value of the second variable y:
1.8
The sum of 3.4000 and 1.8000 is 5.2000
The difference between 3.4000 and 1.8000 is 1.6000
3.4000 divided by 1.8000 is 1.8889

Note the format specifiers in the writeln statement (:0:4), as described in lecture 6.

PROGRAM IntegerCalculations;

VAR x, y, modd, divv: integer;

begin
  WriteLn('Give the value of the first variable x:');
  ReadLn(x);
  Writeln('Give the value of the second variable y:');
  ReadLn(y);
  sum := x + y;
  Writeln('The sum of ', x, ' and ', y, ' is ', sum);
  diff := x - y;
  Writeln('The difference between ', x, ' and ', y, ' is ', diff);
  divv := x Div y;
  modd := x Mod y;
  Writeln(x, ' divided by ', y, ' is ', divis, ' plus ', modd, '/', y);
end.

will produce, when running:

Give the value of the first variable x:
13
Give the value of the second variable y:
5
The sum of 13 and 5 is 18
The difference between 13 and 5 is 8
13 divided by 5 is 2 plus 3/5


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, 1 março 2002