Expressions (Object Pascal)
Go Up to Fundamental Syntactic Elements Index
This topic describes syntax rules of forming Object Pascal expressions.
The following items are covered in this topic:
 Valid Object Pascal Expressions
 Operators
 Function calls
 Set constructors
 Indexes
 Typecasts
Contents
 1 Expressions
 2 Operators
 3 Function Calls
 4 Set Constructors
 5 Indexes
 6 Typecasts
 7 See Also
Expressions
An expression is a construction that returns a value. The following table shows examples of Object Pascal expressions:

variable 

address of the variable X 

integer constant 

variable 

function call 

product of X and Y 

quotient of Z and (1  Z) 

Boolean 

Boolean 

negation of a Boolean 

set 

value typecast 
The simplest expressions are variables and constants (described in Data Types, Variables, and Constants). More complex expressions are built from simpler ones using operators, function calls, set constructors, indexes, and typecasts.
Operators
Operators behave like predefined functions that are part of the Object Pascal language. For example, the expression (X + Y)
is built from the variables X
and Y
, called operands, with the + operator; when X
and Y
represent integers or reals, (X + Y)
returns their sum. Operators include @, not, ^, *, /, div, mod, and, shl, shr, as, +, , or, xor, =, >, <, <>, <=, >=, in, and is.
The operators @, not, and ^ are unary (taking one operand). All other operators are binary (taking two operands), except that + and  can function as either a unary or binary operator. A unary operator always precedes its operand (for example, B
), except for ^, which follows its operand (for example, P^
). A binary operator is placed between its operands (for example, A = 7
).
Some operators behave differently depending on the type of data passed to them. For example, not performs bitwise negation on an integer operand and logical negation on a Boolean operand. Such operators appear below under multiple categories.
Except for ^, is, and in, all operators can take operands of type Variant; for details, see Variant Types.
The sections that follow assume some familiarity with Object Pascal data types; for more information, see Data Types, Variables, and Constants.
For information about operator precedence in complex expressions, see Operator Precedence Rules, later in this topic.
Arithmetic Operators
Arithmetic operators, which take real or integer operands, include +, , *, /, div, and mod.
Binary Arithmetic Operators:
Operator  Operation  Operand Types  Result Type  Example 

+ 
addition 
integer, real 
integer, real 

 
subtraction 
integer, real 
integer, real 

* 
multiplication 
integer, real 
integer, real 

/ 
real division 
integer, real 
real 

div 
integer division 
integer 
integer 

mod 
remainder 
integer 
integer 

Unary arithmetic operators:
Operator  Operation  Operand Type  Result Type  Example 

+ 
sign identity 
integer, real 
integer, real 

 
sign negation 
integer, real 
integer, real 

The following rules apply to arithmetic operators:
 The value of
x / y
is of type Extended, regardless of the types ofx
andy
. For other arithmetic operators, the result is of type Extended whenever at least one operand is a real; otherwise, the result is of type Int64 when at least one operand is of type Int64; otherwise, the result is of type Integer. If an operand's type is a subrange of an integer type, it is treated as if it were of the integer type.  The value of
x div y
is the value ofx / y
rounded in the direction of zero to the nearest integer.  The mod operator returns the remainder obtained by dividing its operands. In other words,
x mod y = x  (x div y) * y
.  A runtime error occurs when
y
is zero in an expression of the formx / y
,x div y
, orx mod y
.
Boolean Operators
The Boolean operators not, and, or, and xor take operands of any Boolean type and return a value of type Boolean.
Boolean Operators:
Operator  Operation  Operand Types  Result Type  Example 

not 
negation 
Boolean 
Boolean 

and 
conjunction 
Boolean 
Boolean 

or 
disjunction 
Boolean 
Boolean 

xor 
exclusive disjunction 
Boolean 
Boolean 

These operations are governed by standard rules of Boolean logic. For example, an expression of the form x and y
is True if and only if both x
and y
are True.
Complete Versus ShortCircuit Boolean Evaluation
The compiler supports two modes of evaluation for the and and or operators: complete evaluation and shortcircuit (partial) evaluation. Complete evaluation means that each conjunct or disjunct is evaluated, even when the result of the entire expression is already determined. Shortcircuit evaluation means strict lefttoright evaluation that stops as soon as the result of the entire expression is determined. For example, if the expression A and B
is evaluated under shortcircuit mode when A
is False, the compiler will not evaluate B
; it knows that the entire expression is False as soon as it evaluates A
.
Shortcircuit evaluation is usually preferable because it guarantees minimum execution time and, in most cases, minimum code size. Complete evaluation is sometimes convenient when one operand is a function with side effects that alter the execution of the program.
Shortcircuit evaluation also allows the use of constructions that might otherwise result in illegal runtime operations. For example, the following code iterates through the string S
, up to the first comma.
while (I <= Length(S)) and (S[I] <> ',') do begin ... Inc(I); end;
In the case where S
has no commas, the last iteration increments I
to a value which is greater than the length of S
. When the while condition is next tested, complete evaluation results in an attempt to read S[I]
, which could cause a runtime error. Under shortcircuit evaluation, in contrast, the second part of the while condition (S[I] <> ',')
is not evaluated after the first part fails.
Use the $B
compiler directive to control evaluation mode. The default state is {$B}
, which enables shortcircuit evaluation. To enable complete evaluation locally, add the {$B+}
directive to your code. You can also switch to complete evaluation on a projectwide basis by selecting Complete Boolean Evaluation in the Compiler Options dialog (all source units will need to be recompiled).
Note: If either operand involves a variant, the compiler always performs complete evaluation (even in the {$B}
state).
Logical (Bitwise) Operators
The following logical operators perform bitwise manipulation on integer operands. For example, if the value stored in X
(in binary) is 001101
and the value stored in Y
is 100001
, the statement:
Z := X or Y;
assigns the value 101101
to Z
.
Logical (Bitwise) Operators:
Operator  Operation  Operand Types  Result Type  Example 

not 
bitwise negation 
integer 
integer 

and 
bitwise and 
integer 
integer 

or 
bitwise or 
integer 
integer 

xor 
bitwise xor 
integer 
integer 

shl 
bitwise shift left 
integer 
integer 

shr 
bitwise shift right 
integer 
integer 

The following rules apply to bitwise operators:
 The result of a not operation is of the same type as the operand.
 If the operands of an and, or, or xor operation are both integers, the result is of the predefined integer type with the smallest range that includes all possible values of both types.
 The operations
x shl y
andx shr y
shift the value ofx
to the left or right byy
bits, which (ifx
is an unsigned integer) is equivalent to multiplying or dividingx
by2^y
; the result is of the same type asx
. For example, ifN
stores the value01101
(decimal 13), thenN shl 1
returns11010
(decimal 26). Note that the value ofy
is interpreted modulo the size of the type ofx
. Thus for example, ifx
is an integer,x shl 40
is interpreted asx shl 8
because an integer is 32 bits and 40 mod 32 is 8.
Example
If x
is a negative integer, the shl
and shr
operations are made clear in the following example:
var x: integer; y: string; ... begin x := 20; x := x shr 1; //As the number is shifted to the right by 1 bit, the sign bit's value replaced is with 0 (all negative numbers have the sign bit set to 1). y := IntToHex(x, 8); writeln(y); //Therefore, x is positive. //Decimal value: 2147483638 //Hexadecimal value: 7FFFFFF6 //Binary value: 0111 1111 1111 1111 1111 1111 1111 0110 end.
String Operators
The relational operators =, <>, <, >, <=, and >= all take string operands (see Relational operators later in this section). The + operator concatenates two strings.
String Operators:
Operator  Operation  Operand Types  Result Type  Example 

+ 
concatenation 
string, packed string, character 
string 

The following rules apply to string concatenation:
 The operands for + can be strings, packed strings (packed arrays of type Char), or characters. However, if one operand is of type WideChar, the other operand must be a long string (UnicodeString, AnsiString, or WideString).
 The result of a + operation is compatible with any string type. However, if the operands are both short strings or characters, and their combined length is greater than 255, the result is truncated to the first 255 characters.
Pointer Operators
 The relational operators <, >, <=, and >= can take operands of type PAnsiChar and PWideChar (see Relational operators). The following operators also take pointers as operands. For more information about pointers, see Pointers and Pointer Types (Object Pascal) in Data Types, Variables, and Constants.
Characterpointer operators:
Operator  Operation  Operand Types  Result Type  Example 

+ 
pointer addition 
character pointer, integer 
character pointer 

 
pointer subtraction 
character pointer, integer 
character pointer, integer 

^ 
pointer dereference 
pointer 
base type of pointer 

= 
equality 
pointer 
Boolean 

<> 
inequality 
pointer 
Boolean 

The ^ operator dereferences a pointer. Its operand can be a pointer of any type except the generic Pointer, which must be typecast before dereferencing.
P = Q
is True just in case P
and Q
point to the same address; otherwise, P <> Q
is True.
You can use the + and  operators to increment and decrement the offset of a character pointer. You can also use  to calculate the difference between the offsets of two character pointers. The following rules apply:
 If
I
is an integer andP
is a character pointer, thenP + I
addsI
to the address given byP
; that is, it returns a pointer to the addressI
characters afterP
. (The expressionI + P
is equivalent toP + I
.)P  I
subtractsI
from the address given byP
; that is, it returns a pointer to the addressI
characters beforeP
. This is true for PAnsiChar pointers; for PWideChar pointersP + I
addsI * SizeOf(WideChar)
toP
.  If
P
andQ
are both character pointers, thenP  Q
computes the difference between the address given byP
(the higher address) and the address given byQ
(the lower address); that is, it returns an integer denoting the number of characters betweenP
andQ
.
P + Q
is not defined.
Set Operators
The following operators take sets as operands.
Set Operators:
Operator  Operation  Operand Types  Result Type  Example 

+ 
union 
set 
set 

 
difference 
set 
set 

* 
intersection 
set 
set 

<= 
subset 
set 
Boolean 

>= 
superset 
set 
Boolean 

= 
equality 
set 
Boolean 

<> 
inequality 
set 
Boolean 

in 
membership 
ordinal, set 
Boolean 

The following rules apply to +, , and *:
 An ordinal
O
is inX + Y
if and only ifO
is inX
orY
(or both).O
is inX  Y
if and only ifO
is inX
but not inY
.O
is inX * Y
if and only ifO
is in bothX
andY
.  The result of a +, , or * operation is of the type
set of A..B
, whereA
is the smallest ordinal value in the result set andB
is the largest.
The following rules apply to <=, >=, =, <>, and in:

X <= Y
is True just in case every member ofX
is a member ofY
;Z >= W
is equivalent toW <= Z
.U = V
is True just in caseU
andV
contain exactly the same members; otherwise,U <> V
is True.  For an ordinal
O
and a setS
,O in S
is True just in caseO
is a member ofS
.
Relational Operators
Relational operators are used to compare two operands. The operators =, <>, <=, and >= also apply to sets.
Relational Operators:
Operator  Operation  Operand Types  Result Type  Example 

= 
equality 
simple, class, class reference, interface, string, packed string 
Boolean 

<> 
inequality 
simple, class, class reference, interface, string, packed string 
Boolean 

< 
lessthan 
simple, string, packed string, PChar 
Boolean 

> 
greaterthan 
simple, string, packed string, PChar 
Boolean 

<= 
lessthanorequalto 
simple, string, packed string, PChar 
Boolean 

>= 
greaterthanorequalto 
simple, string, packed string, PChar 
Boolean 

For most simple types, comparison is straightforward. For example, I = J
is True just in case I
and J
have the same value, and I <> J
is True otherwise. The following rules apply to relational operators.
 Operands must be of compatible types, except that a real and an integer can be compared.
 Strings are compared according to the ordinal values that make up the characters that make up the string. Character types are treated as strings of length 1.
 Two packed strings must have the same number of components to be compared. When a packed string with
n
components is compared to a string, the packed string is treated as a string of lengthn
.  Use the operators <, >, <=, and >= to compare PAnsiChar (and PWideChar) operands only if the two pointers point within the same character array.
 The operators = and <> can take operands of class and classreference types. With operands of a class type, = and <> are evaluated according the rules that apply to pointers:
C = D
is True just in caseC
andD
point to the same instance object, andC <> D
is True otherwise. With operands of a classreference type,C = D
is True just in caseC
andD
denote the same class, andC <> D
is True otherwise. This does not compare the data stored in the classes. For more information about classes, see Classes and Objects.
Class and Interface Operators
The operators as and is take classes and instance objects as operands; as operates on interfaces as well. For more information, see Classes and Objects, Object Interfaces and Interface References.
The relational operators = and <> also operate on classes.
The @ Operator
The @ operator returns the address of a variable, or of a function, procedure, or method; that is, @ constructs a pointer to its operand. For more information about pointers, see "Pointers and Pointer Types" in Data Types, Variables, and Constants. The following rules apply to @.
 If
X
is a variable,@X
returns the address ofX
. (Special rules apply whenX
is a procedural variable; see "Procedural Types in Statements and Expressions" in Data Types, Variables, and Constants.) The type of@X
is Pointer if the default{$T}
compiler directive is in effect. In the{$T+}
state,@X
is of type^T
, whereT
is the type ofX
(this distinction is important for assignment compatibility, see Assignmentcompatibility).  If
F
is a routine (a function or procedure),@F
returnsF
's entry point. The type of@F
is always Pointer.  When @ is applied to a method defined in a class, the method identifier must be qualified with the class name. For example,
@TMyClass.DoSomething
 points to the
DoSomething
method ofTMyClass
. For more information about classes and methods, see Classes and Objects.
Note: When using the @ operator, it is not possible to take the address of an interface method, because the address is not known at compile time and cannot be extracted at run time.
Operator Precedence
In complex expressions, rules of precedence determine the order in which operations are performed.
Precedence of operators
Operators  Precedence 

@ 
first (highest) 
* 
second 
+ 
third 
= 
fourth (lowest) 
An operator with higher precedence is evaluated before an operator with lower precedence, while operators of equal precedence associate to the left. Hence the expression:
X + Y * Z
multiplies Y
times Z
, then adds X
to the result; * is performed first, because is has a higher precedence than +. But:
X  Y + Z
first subtracts Y
from X
, then adds Z
to the result;  and + have the same precedence, so the operation on the left is performed first.
You can use parentheses to override these precedence rules. An expression within parentheses is evaluated first, then treated as a single operand. For example:
(X + Y) * Z
multiplies Z
times the sum of X
and Y
.
Parentheses are sometimes needed in situations where, at first glance, they seem not to be. For example, consider the expression:
X = Y or X = Z
The intended interpretation of this is obviously:
(X = Y) or (X = Z)
Without parentheses, however, the compiler follows operator precedence rules and reads it as:
(X = (Y or X)) = Z
which results in a compilation error unless Z
is Boolean.
Parentheses often make code easier to write and to read, even when they are, strictly speaking, superfluous. Thus the first example could be written as:
X + (Y * Z)
Here the parentheses are unnecessary (to the compiler), but they spare both programmer and reader from having to think about operator precedence.
Function Calls
Because functions return a value, function calls are expressions. For example, if you have defined a function called Calc
that takes two integer arguments and returns an integer, then the function call Calc(24,47)
is an integer expression. If I
and J
are integer variables, then I + Calc(J,8)
is also an integer expression. Examples of function calls include:
Sum(A, 63) Maximum(147, J) Sin(X + Y) Eof(F) Volume(Radius, Height) GetValue TSomeObject.SomeMethod(I,J);
For more information about functions, see Procedures and Functions.
Set Constructors
A set constructor denotes a settype value. For example:
[5, 6, 7, 8]
denotes the set whose members are 5, 6, 7, and 8. The set constructor:
[ 5..8 ]
could also denote the same set.
The syntax for a set constructor is:
[ item1, ..., itemn ]
where each item is either an expression denoting an ordinal of the set's base type or a pair of such expressions with two dots (..) in between. When an item has the form x..y
, it is shorthand for all the ordinals in the range from x
to y
, including y
; but if x
is greater than y
, then x..y
, the set [x..y]
, denotes nothing and is the empty set. The set constructor [ ]
denotes the empty set, while [x]
denotes the set whose only member is the value of x
.
Examples of set constructors:
[red, green, MyColor] [1, 5, 10..K mod 12, 23] ['A'..'Z', 'a'..'z', Chr(Digit + 48)]
For more information about sets, see Structured Types in Data Types, Variables, and Constants.
Indexes
Strings, arrays, array properties, and pointers to strings or arrays can be indexed. For example, if FileName
is a string variable, the expression FileName[3]
returns the third character in the string denoted by FileName
, while FileName[I + 1]
returns the character immediately after the one indexed by I
. For information about strings, see Data Types, Variables and Constants. For information about arrays and array properties, see Arrays in Data Types, Variables, and Constants and "Array Properties" in Properties page.
Typecasts
It is sometimes useful to treat an expression as if it belonged to different type. A typecast allows you to do this by, in effect, temporarily changing an expression's type. For example, Integer('A')
casts the character A
as an integer.
The syntax for a typecast is:
typeIdentifier(expression)
If the expression is a variable, the result is called a variable typecast; otherwise, the result is a value typecast. While their syntax is the same, different rules apply to the two kinds of typecast.
Value Typecasts
In a value typecast, the type identifier and the cast expression must both be ordinal or pointer types. Examples of value typecasts include:
Integer('A') Char(48) Boolean(0) Color(2) Longint(@Buffer)
The resulting value is obtained by converting the expression in parentheses. This may involve truncation or extension if the size of the specified type differs from that of the expression. The expression's sign is always preserved.
The statement:
I := Integer('A');
assigns the value of Integer('A')
, which is 65, to the variable I
.
A value typecast cannot be followed by qualifiers and cannot appear on the left side of an assignment statement.
Variable Typecasts
You can cast any variable to any type, provided their sizes are the same and you do not mix integers with reals. (To convert numeric types, rely on standard functions like Int
and Trunc
.) Examples of variable typecasts include:
Char(I) Boolean(Count) TSomeDefinedType(MyVariable)
Variable typecasts can appear on either side of an assignment statement. Thus:
var MyChar: char; ... Shortint(MyChar) := 122;
assigns the character z
(ASCII 122) to MyChar
.
You can cast variables to a procedural type. For example, given the declarations:
type Func = function(X: Integer): Integer; var F: Func; P: Pointer; N: Integer;
you can make the following assignments:
F := Func(P); { Assign procedural value in P to F } Func(P) := F; { Assign procedural value in F to P } @F := P; { Assign pointer value in P to F } P := @F; { Assign pointer value in F to P } N := F(N); { Call function via F } N := Func(P)(N); { Call function via P }
Variable typecasts can also be followed by qualifiers, as illustrated in the following example:
type TByteRec = record Lo, Hi: Byte; end; TWordRec = record Low, High: Word; end; PByte = ^Byte; var B: Byte; W: Word; L: Longint; P: Pointer; begin W := $1234; B := TByteRec(W).Lo; TByteRec(W).Hi := 0; L := $1234567; W := TWordRec(L).Low; B := TByteRec(TWordRec(L).Low).Hi; B := PByte(L)^; end;
In this example, TByteRec
is used to access the low and highorder bytes of a word, and TWordRec
to access the low and highorder words of a long integer. You could call the predefined functions Lo
and Hi
for the same purpose, but a variable typecast has the advantage that it can be used on the left side of an assignment statement.
For information about typecasting pointers, see Pointers and Pointer Types (Object Pascal). For information about casting class and interface types, see "The as Operator" in Class References and Interface References.