There are a number of expression and assignment operators in Pascal. Our object in this section is to cover the more important of these.
Because of the variety of Pascal operators, it is important to understand the precedence that each operator has relative to the others when the compiler is evaluating an expression. It is good coding practice to remove any ambiguity by placing explicit parentheses around expressions, thereby specifically identifying the precedence of evaluation within that expression. The table below provides a summary of Pascal operators in order from the highest (in the top section of the table) to the lowest precedence. Operators within the same section have the same precedence. Some of the operators listed we have not yet come across, but they are listed for completeness here.
Operator Summary
Operator
Purpose
[]
Array Subscript
NOT
Logical negation for boolean variables or bitwise negation for integer variables
*
/
div
mod
AND
Multiply
Divide
Integer divide
Modulus from integer division
Logical AND for boolean variables or bitwise AND for integer variables
+
-
OR
Numerical addition, Set union or String concatenation
Numerical subtraction or Set difference
Logical exclusive OR for boolean variables or bitwise exclusive or for integer variables
<
<=
=
<>
>=
>
Less than
Less than or equal to
Equal to
Not equal to
Greater than or equal to
Greater than
Where an expression uses operators of the same precedence, they are evaluated from left to right (left associative). Parentheses override the standard precedence of operation, with innermost parentheses taking the highest priority.
Example
program precedence1;
var a, b, c : real;
begin
a := 10;
b := 12;
c := 7;
writeln ('a has the value ',a:4);
writeln ('b has the value ',b:4);
writeln ('c has the value ',c:4);
a := (a * b) / c;
writeln ('a now has the value ',a:5:2);
end.
We have come across all of the code structures used above before, with the exception of the line
a := (a * b) / c;
In this case, the value (a * b) is stored in an intermediate field whose attributes are based on the attributes of the operands to the calculation. This intermediate value is then divided by c, and the result of this operation then assigned to the variable a.
Binary Operations and Assignments
Examples of the binary operations AND, OR, and NOT are shown below :
Expression
Returns
x = (0<=testvar AND testvar<=9)
x = TRUE if testvar is in the range 0 to 9, else 0
y = (0>testvar OR (testvar >9))
y = TRUE if testvar is less than 0 or greater than 9, else 0
z = (NOT(testvar = 7))
z = FALSE if testvar = 7, else 1
In the above examples, it is assumed that x, y and z have all been declared to be boolean variables. Note the importance of the parentheses in deciding the sequence of evaluation.
It can be seen that in the above example, y = NOT(x).
When checking for equality in a test, it is important not to inadvertantly use the assignment (:=) operator instead checking for equality (=). The compiler will throw up an error if this mistake is made, e.g.
program precedence2;
var a: integer;
begin
a := 3;
if (a = 99) then writeln ('This writeln command will never be executed');
if (a = 3) then writeln ('a has the value 3');
(* if (a := 3) then writeln ('This command would throw up a compiler error'); *)
end.
For completeness, where the variables A and B both evaluate to a TRUE or FALSE, the result of the AND, OR and NOT operators on the variables is shown below.
A
B
A AND B
A OR B
NOT(A)
FALSE
FALSE
FALSE
FALSE
TRUE
FALSE
TRUE
FALSE
TRUE
TRUE
TRUE
FALSE
FALSE
TRUE
FALSE
TRUE
TRUE
TRUE
TRUE
FALSE
The most common use of binary operators is in combining comparisons. Usually the result of the comparison will result in a program taking one or another course of action depending on the result of the comparison, thereby regulating the flow of control of the program.
Example
A trivial example of the use of binary operators is included below :
program precendence3;
var a, b : boolean;
begin
a := TRUE;
b := FALSE;
writeln ('a is ',a,' b is ',b);
writeln ('a AND b gives ', A AND B);
writeln ('a OR b gives ', A OR B);
writeln ('NOT a gives ', NOT(A),', NOT b gives ',NOT(b));
writeln ('NOT (a OR b) gives ', NOT (a OR b));
writeln ('(NOT a) or b gives ', (NOT a) OR b);
writeln ('NOT (a AND b) gives ', NOT (a AND b));
writeln ('(NOT a) AND b gives ', (NOT a) AND b);
end.