Operator precedence
Pascal has four levels of precedence, which is fewer than most languages. The result is
that comparisons need parentheses around the expressions they compare:
(A > 0) and (B > 0), because and binds tighter than
>. Operators of the same level are applied from left to right.
| Level | Operators | Kind |
|---|---|---|
| 1 (highest) | @ not and unary + - | address of, logical or bitwise not, sign |
| 2 | * / div mod and shl shr as | multiplying operators |
| 3 | + - or xor | adding operators |
| 4 (lowest) | = <> < > <= >= in is | relational operators |
ISO 7185 has these levels with not at the first level, * / div mod and at the
second, + - or and the sign at the third and the relational operators at the
fourth: in the standard the unary minus has the precedence of the adding operators
(-A * B is -(A * B)), which Free Pascal’s iso and Mac Pascal modes
follow (isounaryminus). Turbo Pascal and Delphi bind the sign
tighter, as the table shows. shl, shr and xor are Turbo
Pascal’s, is and as Delphi’s. Free Pascal adds **
(power), << and >> (shifts, as shl and
shr) and >< (symmetric set difference); where they stand in the
table is in its Reference guide, which was not read for this.
Arithmetic
+ - *on integers give an integer, on reals a real, and on a mix a real./divides and always gives a real (7 / 2 = 3.5).divis integer division truncating toward zero (-7 div 2 = -3).modis the remainder with the sign of the left operand in Turbo Pascal, Delphi and Free Pascal; ISO 7185 definesmodwith a non-negative result (isomodmode switch).**is the power operator in Free Pascal (2 ** 10), and in Extended Pascal, wherepowalso raises to an integer power.- Overflow checking (
{$Q+}) turns integer overflow into a run-time error; otherwise integers wrap around.
Boolean and bitwise
not, and, or and xor work on booleans, and on
integers bit by bit. shl and shr shift integers. With
{$B-} (the default in Turbo Pascal, Delphi and Free Pascal) and and
or stop evaluating as soon as the result is known (short-circuit
evaluation): (P <> nil) and (P^.X > 0) is safe. With {$B+} both sides are
always evaluated. ISO 7185 leaves it to the compiler whether the right side is evaluated, so a
portable program must not depend on either. Extended Pascal adds and_then and
or_else, which always short-circuit.
Relational and set operators
= <> < > <= >=compare numbers, characters, strings (by character code, left to right), booleans, enumerations (by order), pointers (=and<>only) and sets (<=is subset,>=superset).X in Sis true when the ordinal value X is in the set S, or the range or enumerated values it names (C in ['a'..'z', '_']).A is Tis true if the object A is of class T or a descendant, andA as Tcasts it, raising an exception if it is not.- Sets:
+union,-difference,*intersection,><symmetric difference (Free Pascal).
Strings, pointers and addresses
+joins strings and characters:'a' + 'b',S + #13#10.@Xis the address of a variable, routine or method;@@Xthe address of a procedural variable itself.{$T+}makes@Xa typed pointer,{$T-}(the default) an untypedPointer.Addr(X)does the same as@X.P^is the value a pointer or file buffer points at.P^.Fieldreaches a field, and with automatic dereferencing (autoderef, Delphi)P.Fielddoes too.- Pointer arithmetic:
P + 1,Inc(P), with{$POINTERMATH ON}or onPChar; the step is the size of the pointed-at type.
Variables, calls and constructors
- A variable may be qualified:
A[I, J],R.Field,P^.Next^.Value,Obj.Method.Property,S[I]for a string character. - A function call is its name and its arguments in parentheses; a function without parameters may be called without parentheses (
Now). - A set constructor is a list in brackets:
[],[1, 3..5],[Red, Blue]. - An array constructor in Delphi and Free Pascal is also written in brackets and takes the type of its context:
Sum([1, 2, 3])for an open array,Format('%d %s', [1, 'a'])forarray of const. - A record constant or typed constant is written in parentheses with field names:
(X: 1; Y: 2). - Literals are numbers, strings, characters,
nil,TrueandFalse. - Self, Result and inherited:
Selfis the object of the current method,Resultthe variable that holds a function’s result (Object Pascal modes), andinherited Method(args)calls the parent class’s method. - Anonymous methods are expressions:
function(X: Integer): Integer begin Result := X * 2; end(Delphi 2009 and later; Free Pascal development version with theanonymousfunctionsmode switch). - The
if … then … elseform is a statement in Pascal, not an expression; the Free Pascal development version has astatementexpressionsmode switch that lets some statements be used as expressions.
Type casts and conversion
C := Char(65); { value cast }
I := Integer(Colour); { enumeration to integer }
P := PByte(Buffer); { pointer cast }
Obj := TButton(Sender); { unchecked class cast }
Btn := Sender as TButton; { checked class cast }
X := Double(I); { integer to real: a conversion, not a reinterpretation }
B := Byte(S[1]); { variable cast: may be assigned to: Byte(X) := 1 }
Ord(C); Chr(65); { standard functions for ordinal conversion }
Round(2.5); Trunc(2.7); { real to integer }
A cast of a variable (Byte(X)) is itself a variable, so it may be assigned
to and passed as a var parameter. A value cast changes the type without
changing the bits, except between integers and reals where it converts. Assignment
converts without a cast where the types are compatible. Interface and class casts with
as are checked; Free Pascal and Delphi also have Supports and
QueryInterface for interfaces.
Constant expressions
An expression that is built from constants, literals and certain standard functions is
evaluated by the compiler, and may be used in const and type
declarations, array bounds, case labels and set ranges. The standard functions allowed
are Abs, Chr, Hi, High, Length,
Lo, Low, Odd, Ord, Pred,
Round, SizeOf, Succ, Swap and
Trunc, plus Ptr and Addr for addresses. Free Pascal
and Delphi extend this to more functions (Sqr, Sqrt, Sin,
Cos, Ln and the Exp family, UpCase, Copy,
Pos, Concat and others). Conditional compilation’s
{$IF} expressions are a second, smaller, language (see
Compiler directives).