Pascal operators and expressions
last modified November 18, 2026 Free Pascal 3.2.2
An expression combines values with operators. Pascal has arithmetic operators, string concatenation, comparison operators, Boolean operators, and bitwise operators, and several of them share the same symbol. This article describes the operators, their precedence, the difference between real and integer division, and the short-circuit behaviour of Boolean expressions.
Arithmetic operators
The arithmetic operators are +, -, *,
/, div, and mod. The first three work
on integers and reals. The slash always produces a real, even when both
operands are integers, while div produces the integer quotient
and mod the remainder.
program arithmetic;
{$mode objfpc}{$H+}
var
a, b: Integer;
begin
a := 17;
b := 5;
WriteLn('a + b = ', a + b);
WriteLn('a - b = ', a - b);
WriteLn('a * b = ', a * b);
WriteLn('a / b = ', a / b:0:2);
WriteLn('a div b= ', a div b);
WriteLn('a mod b= ', a mod b);
end.
17 / 5 is 3.4 and is printed with two decimals, while
17 div 5 is 3 and 17 mod 5 is 2. The result of
/ is a real even when it is a whole number, so 10 / 2
prints as 5.0 in default real formatting. Mixing integers and
reals in one expression promotes the integers to reals.
$ fpc -gl arithmetic.pas $ ./arithmetic a + b = 22 a - b = 12 a * b = 85 a / b = 3.40 a div b= 3 a mod b= 2
Integer division and remainders
div truncates toward zero, and mod takes the sign of
the dividend. This is the same rule as C and Java, but it is not the same as a
floor division, where the quotient would move toward negative infinity. The
difference appears when one of the operands is negative.
program division;
{$mode objfpc}{$H+}
begin
WriteLn('-7 div 3 = ', -7 div 3);
WriteLn(' 7 div -3 = ', 7 div -3);
WriteLn('-7 div -3 = ', -7 div -3);
WriteLn('-7 mod 3 = ', -7 mod 3);
WriteLn(' 7 mod -3 = ', 7 mod -3);
WriteLn(' 7 / 2 = ', 7 / 2:0:2);
end.
The quotient of -7 and 3 is -2, not -3, and the remainder is -1, so the
identity a = (a div b) * b + (a mod b) still holds. A division by
a variable that is zero raises EDivByZero at run time, while a
constant zero divisor in integer division is rejected at compile time. The real division
7 / 2 produces 3.5 and never truncates.
$ fpc -gl division.pas $ ./division -7 div 3 = -2 7 div -3 = -2 -7 div -3 = 2 -7 mod 3 = -1 7 mod -3 = 1 7 / 2 = 3.50
String concatenation
The plus sign also joins two strings. The result is a new string that contains
the left operand followed by the right one, and the operands may be string
variables, string literals, or single characters. Pascal has no compound
assignment operator, so a string is extended with
s := s + 'more', not with s += 'more'.
program concat;
{$mode objfpc}{$H+}
var
first, last, name: string;
begin
first := 'Grace';
last := 'Hopper';
name := first + ' ' + last;
WriteLn(name);
WriteLn('length: ', Length(name));
WriteLn('first char: ', name[1]);
end.
The expression builds the full name from two variables and a literal space. A
string is indexed from 1, so name[1] is the first character. The
plus operator does not convert numbers to strings automatically; use
IntToStr or Format when a number is part of the
message.
$ fpc -gl concat.pas $ ./concat Grace Hopper length: 12 first char: G
Comparison operators
The comparison operators are =, <>,
<, >, <=, and
>=. They return a Boolean. The equals sign compares values;
assignment uses :=. Strings are compared character by character
according to their byte values, and a shorter string is not padded with blanks
before the comparison.
program comparisons;
{$mode objfpc}{$H+}
begin
WriteLn('5 = 5: ', 5 = 5);
WriteLn('5 <> 4: ', 5 <> 4);
WriteLn('5 < 4: ', 5 < 4);
WriteLn('5 >= 5: ', 5 >= 5);
WriteLn('abc = abd: ', 'abc' = 'abd');
WriteLn('abc < abd: ', 'abc' < 'abd');
WriteLn('abc = abc : ', 'abc' = 'abc ');
WriteLn('a < B: ', 'a' < 'B');
end.
'abc' and 'abc ' are different because the second
string has a trailing blank. 'a' < 'B' is false because the
lower-case letter has a larger byte value than the upper-case one. Comparisons
are case-sensitive; use SameText or CompareText from
SysUtils for a case-insensitive comparison.
$ fpc -gl comparisons.pas $ ./comparisons 5 = 5: TRUE 5 <> 4: TRUE 5 < 4: FALSE 5 >= 5: TRUE abc = abd: FALSE abc < abd: TRUE abc = abc : FALSE a < B: FALSE
Boolean operators
The Boolean operators are and, or, xor,
and not. They work on Boolean values and return a
Boolean. The same symbols also work on integers, where they
perform bitwise operations, which is the subject of a later section.
program boolops;
{$mode objfpc}{$H+}
var
a, b: Boolean;
begin
a := True;
b := False;
WriteLn('a and b: ', a and b);
WriteLn('a or b: ', a or b);
WriteLn('a xor b: ', a xor b);
WriteLn('not a: ', not a);
WriteLn('1 = 1: ', (1 = 1) and (2 = 2));
end.
and is true only when both operands are true, or is
true when at least one is, and xor is true when exactly one is.
not reverses the value. The last line needs parentheses around
the comparisons; without them the compiler applies and before the
comparison and reports a type error. This is explained under precedence below.
$ fpc -gl boolops.pas $ ./boolops a and b: FALSE a or b: TRUE a xor b: TRUE not a: FALSE 1 = 1: TRUE
Short-circuit evaluation
By default, Pascal evaluates and and or from left to
right and stops as soon as the result is known. If the left side of
and is false, the right side is not evaluated; if the left side of
or is true, the right side is not evaluated. This is called
short-circuit evaluation and it makes guard expressions safe.
program shortcircuit;
{$mode objfpc}{$H+}
var
i: Integer;
begin
i := 0;
WriteLn((i <> 0) and (10 div i = 1));
WriteLn('still running');
end.
The left side is false, so 10 div i is never evaluated and the
program does not divide by zero. With the directive {$B+} placed
at the top of the file, both sides are evaluated and the same program stops
with runtime error 200. The default is {$B-}, and the articles in
this series rely on it when they guard a division or a pointer access with a
test.
$ fpc -gl shortcircuit.pas $ ./shortcircuit FALSE still running
Bitwise operators
When the operands are integers, and, or,
xor, and not operate on the individual bits.
shl shifts left and shr shifts right. A left shift
by n is a multiplication by two to the power n; a right shift is a division by
a power of two, but it is a logical shift even for a signed value, so the sign
bit is not preserved.
program bits;
{$mode objfpc}{$H+}
uses SysUtils;
var
i: Integer;
begin
i := -8;
WriteLn('12 and 10 = ', 12 and 10);
WriteLn('12 or 10 = ', 12 or 10);
WriteLn('12 xor 10 = ', 12 xor 10);
WriteLn('not 12 = ', not 12);
WriteLn('1 shl 4 = ', 1 shl 4);
WriteLn('i shr 1 = ', i shr 1);
WriteLn('BinStr = ', BinStr(10, 8));
end.
12 is 1100 in binary and 10 is 1010. Their bitwise
and is 1000, or 8; their or is 1110, or 14; and
their xor is 0110, or 6. not 12 flips all bits of the integer
and produces -13 in two's complement. i shr 1 on -8 produces
2147483644 because the sign bit is filled with zero, and BinStr
prints a number in binary with a chosen width.
$ fpc -gl bits.pas $ ./bits 12 and 10 = 8 12 or 10 = 14 12 xor 10 = 6 not 12 = -13 1 shl 4 = 16 i shr 1 = 2147483644 BinStr = 00001010
Operator precedence
Precedence decides which operator is applied first when an expression has no
parentheses. Pascal has four levels. The unary operators bind tightest, then
multiplication and the Boolean and, then addition and the Boolean
or, and finally the comparisons.
| Level | Operators |
|---|---|
| unary | @, not, unary - |
| multiplicative | *, /, div, mod, and, shl, shr, as |
| additive | +, -, or, xor |
| relational | =, <>, <, >, <=, >=, in, is |
Because and binds tighter than the comparisons, the expression
1 > 0 and 2 > 0 is read as
1 > (0 and 2) > 0 and is rejected. Write
(1 > 0) and (2 > 0) instead. Arithmetic does what most
readers expect: 2 + 3 * 4 is 14, and parentheses change it to 20.
program precedence;
{$mode objfpc}{$H+}
begin
WriteLn('2 + 3 * 4 = ', 2 + 3 * 4);
WriteLn('(2 + 3) * 4 = ', (2 + 3) * 4);
WriteLn('10 - 4 - 3 = ', 10 - 4 - 3);
WriteLn('16 div 4 * 2 = ', 16 div 4 * 2);
WriteLn('1 + 2 = 3 = ', 1 + 2 = 3);
end.
Subtraction and division are left-associative, so 10 - 4 - 3 is
(10 - 4) - 3 and 16 div 4 * 2 is
(16 div 4) * 2. The last line shows that the comparison is
applied after the addition, which is why it prints TRUE.
$ fpc -gl precedence.pas $ ./precedence 2 + 3 * 4 = 14 (2 + 3) * 4 = 20 10 - 4 - 3 = 3 16 div 4 * 2 = 8 1 + 2 = 3 = TRUE
Inc and Dec
Inc and Dec add or subtract from an ordinal variable
in place. They are procedures, not operators, but they are used so often that
they belong with the arithmetic operators. Inc(x) adds one and
Inc(x, n) adds n; Dec subtracts in the same way.
program incdec;
{$mode objfpc}{$H+}
var
i: Integer;
begin
i := 10;
Inc(i);
WriteLn('after Inc: ', i);
Inc(i, 5);
WriteLn('after Inc(i,5): ', i);
Dec(i, 3);
WriteLn('after Dec(i,3): ', i);
end.
The variable is passed by reference, so Inc changes the caller's
variable directly. The two-argument form is equivalent to
i := i + 5 but usually compiles to a shorter instruction. The same
procedures work on characters and enumerations, where they move to the next or
previous value in the ordinal order.
$ fpc -gl incdec.pas $ ./incdec after Inc: 11 after Inc(i,5): 16 after Dec(i,3): 13
Next steps
The conditionals article uses comparisons
and Boolean operators in if and case statements. The
loops article uses integer expressions as bounds,
and the numeric functions article
collects the routines that operate on the values produced by these
expressions.
Source
In this article we have covered Pascal arithmetic, integer division, string
concatenation, comparisons, Boolean and bitwise operators, short-circuit
evaluation, precedence, and the Inc and Dec
procedures. The series continues with
Pascal conditionals.
Author
List all Pascal tutorials.