Integer Variables

Integers are whole numbers, such as 1, 6, and 103. In the following program the + operator adds the integers, as you would expect. (The same operator concatenates strings).

In these simple demonstration programs there is no validation to check that the data entered is appropriate. We cover this important feature of good programming in another tutorial.

program Integers;
  Num1, Num2, Sum, Difference, Product : integer;
  write('Please enter a whole number between 1 and 100 ');
  write('Please enter another whole number between 1 and 100 ');
  Sum := Num1 + Num2;
  Difference := Num1 - Num2;
  Product := Num1 * Num2;
  writeln(Num1, ' + ', Num2, ' = ', Sum);
  writeln(Num1, ' - ', Num2, ' = ', Difference);
  writeln(Num1, ' * ', Num2, ' = ', Product);

The program above could not have an integer variable to store the result of dividing one number by another. Even though the division might result in a whole number, an integer variable is only allowed if the result must be a whole number. (The results of additions, subtractions and products of integers must be whole numbers, so the program runs as expected).

There are several types of variable that hold whole numbers, some of which are signed, meaning that they can store positive and negative whole numbers.

Features introduced:
  • Integer variables
  • Use of the +, - and * operators, for addition, subtraction and multiplication, respectively
Programming - a skill for life!

The various types of constants and variables and how to use them