Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 36 ยท Basics and Types

Operators and Expressions in Pascal

A complete guide to Pascal operators: arithmetic, relational, logical, bitwise shl and shr, string and set operators, precedence levels, type promotion and common expression mistakes.

An expression combines values, variables and function calls with operators to produce a new value. You have already used +, := and div. This lesson collects all of Pascal's operators in one place, explains the unusual precedence table (it has only four levels) and shows how to avoid the classic mistakes.

Pascal examples are read-only. The runnable parts are C stand-ins, which are especially natural here because C has the same bitwise operators under different names.

Arithmetic operators

Operator Meaning Operands Result
+ - * Add, subtract, multiply Integer or real Integer or real
/ Real division Integer or real Real
div Integer division Integer Integer
mod Remainder Integer Integer
unary - + Sign Integer or real Same

Sqr(x) squares and Sqrt(x) takes the square root. Powers need the Math unit: Power(2, 10) is 1024.0. There is no ^ operator for powers in Pascal (that symbol is for pointers).

Relational operators

=, <> (not equal), <, >, <=, >= compare two values and give a Boolean. in tests set membership: c in ['a'..'z']. These work on numbers, characters, strings, booleans and enumerations.

Logical and bitwise operators

The keywords and, or, xor, not do two jobs depending on what you give them:

  • On Boolean operands they are logical: True and False is False.
  • On integer operands they work bit by bit: 12 and 10 is 8.
PASCAL
var
  flags: Byte;
begin
  flags := 0;
  flags := flags or 4;           { set bit 2: now 4 }
  flags := flags or 1;           { set bit 0: now 5 }
  WriteLn((flags and 4) <> 0);   { TRUE: bit 2 is set }
  flags := flags and not 1;      { clear bit 0: now 4 }
  WriteLn(flags);                { 4 }
end.

Here the parentheses around flags and 4 are optional, because Pascal's relational operators have the lowest precedence: flags and 4 <> 0 already means (flags and 4) <> 0. The danger is the other direction: in x > 0 and x < 10 the and grabs 0 and x first. Parenthesize anyway, so a reader never has to remember the table.

Shifts use shl and shr:

PASCAL
WriteLn(1 shl 3);     { 8: shift left three places }
WriteLn(256 shr 4);   { 16 }

String and set operators

+ concatenates strings (and characters). Sets use + for union, - for difference and * for intersection:

PASCAL
var a, b: set of 1..9;
begin
  a := [1, 2, 3];
  b := [3, 4];
  { a + b = [1,2,3,4], a * b = [3], a - b = [1,2] }
end.

Precedence: four levels

Pascal's table is short, which surprises people used to C:

Level Operators Notes
1 (highest) not, unary -, unary +, @
2 *, /, div, mod, and, shl, shr multiplicative
3 +, -, or, xor additive
4 (lowest) =, <>, <, >, <=, >=, in relational
flowchart TD
    A["1. not, unary minus"] --> B["2. * / div mod and shl shr"]
    B --> C["3. + - or xor"]
    C --> D["4. = <> < > <= >= in"]

Figure: higher rows bind tighter. Operators on the same level are evaluated left to right.

Examples:

  • 2 + 3 * 4 is 14 because * is on a higher level than +.
  • a or b = c parses as (a or b) = c, because or is on level 3 and = on level 4. That is rarely what you meant.
  • x > 0 and x < 10 fails because and wins first. Write (x > 0) and (x < 10).

The golden rule: parenthesize anything mixing logical and relational operators.

Type promotion in expressions

  • Integer with integer: the result is an integer (a wider type if needed).
  • Integer with real: the integer becomes real.
  • / always produces a real.
  • A Char plus a Char is a string: 'a' + 'b' is 'ab'.
PASCAL
var
  i: Integer;
  r: Double;
begin
  i := 7;
  r := i / 2;        { 3.5 }
  i := i div 2;      { 3 }
  r := i * 1.5;      { 4.5 }
end.

Increment and compound updates

Pascal has no ++ or += operator in standard syntax. Use Inc and Dec (clear and efficient), or assign:

PASCAL
Inc(count);        { count := count + 1 }
Inc(count, 5);     { count := count + 5 }
Dec(count);
total := total * 2;

Free Pascal accepts count += 1 when the {$COPERATORS ON} switch is set (enabled in objfpc and delphi modes of newer versions), but Inc is universal.

Short-circuit evaluation

By default Free Pascal and Delphi evaluate and and or left to right and stop as soon as the result is known ({$B-}):

PASCAL
if (i <= High(list)) and (list[i] > 0) then ...

If i is out of range the second test never runs. Do not rely on side effects in conditions, and remember standard Pascal did not promise this behavior.

Stand-ins in C

The following programs are C, mirroring and, or, xor, shl and shr, which in C are &, |, ^, << and >>.

C C stand-in: setting and testing bits

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C C stand-in: precedence

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the value of 2 + 3 * 4 in Pascal?

    Multiplication is on a higher precedence level than addition, so it is evaluated first (3 * 4 = 12, then 2 + 12 = 14).
  2. 2. What do and, or, xor do when given integers?

    With integer operands the keywords are bitwise operators. With booleans they are logical.
  3. 3. Why is if x > 0 and x < 10 wrong?

    and binds first, so 0 and x is evaluated as an integer operation and the whole thing fails to type-check. Parenthesize each comparison.
  4. 4. Which Pascal statement adds 1 to count in standard syntax?

    Inc(count) (or count := count + 1) is the standard form. ++ does not exist.

Summary

  • Arithmetic: + - *, / for reals, div and mod for integers.
  • and, or, xor, not are logical on booleans and bitwise on integers; shl and shr shift bits.
  • Only four precedence levels; always parenthesize comparisons combined with and/or.
  • Use Inc and Dec; and/or short-circuit in modern compilers.

Next: Enumerations, Subranges and Sets