Operators can be classified according to the type of expression they operate on. We will discuss them type by type.
Arithmetic operators occur in arithmetic operations, i.e. in expressions that contain integers or reals. There are 2 kinds of operators : Binary and unary arithmetic operators.
Binary operators are listed in table (), unary operators are
listed in table (
).
Operator | Operation |
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
Div | Integer division |
Mod | Remainder |
With the exception of Div and Mod, which accept only integer expressions as operands, all operators accept real and integer expressions as operands.
For binary operators, the result type will be integer if both operands are integer type expressions. If one of the operands is a real type expression, then the result is real.
As an exception : division (/) results always in real values.
Operator | Operation |
+ | Sign identity |
- | Sign inversion |
For unary operators, the result type is always equal to the expression type.
The division (/) and Mod operator will cause run-time errors if the second argument is zero.
The sign of the result of a Mod operator is the same as the sign of
the left side operand of the Mod operator. In fact, the Mod
operator is equivalent to the following operation :
but it executes faster than the right hand side expression.
Logical operators act on the individual bits of ordinal expressions.
Logical operators require operands that are of an integer type, and produce
an integer type result. The possible logical operators are listed in
table ().
Operator | Operation |
not | Bitwise negation (unary) |
and | Bitwise and |
or | Bitwise or |
xor | Bitwise xor |
shl | Bitwise shift to the left |
shr | Bitwise shift to the right |
The following are valid logical expressions:
Boolean operators can be considered logical operations on a type with 1 bit size. Therefore the shl and shr operations have little sense.
Boolean operators can only have boolean type operands, and the resulting
type is always boolean. The possible operators are listed in
table ()
Operator | Operation |
not | logical negation (unary) |
and | logical and |
or | logical or |
xor | logical xor |
Remark that boolean expressions are ALWAYS evaluated with short-circuit evaluation. This means that from the moment the result of the complete expression is known, evaluation is stopped and the result is returned.
For instance, in the following expression:
The compiler will never look at the value of MaybeTrue, since it is
obvious that the expression will always be true. As a result of this
strategy, if MaybeTrue is a function, it will not get called !
(This can have surprising effects when used in conjunction with properties)
There is only one string operator : +. It's action is to concatenate the contents of the two strings (or characters) it stands between.
You cannot use + to concatenate null-terminated (PChar) strings.
The following are valid string operations:
The following is not:
Because Dirname is a null-terminated string.
The following operations on sets can be performed with operators:
Union, difference and intersection. The operators needed for this are listed
in table ().
Operator | Action |
+ | Union |
- | Difference |
* | Intersection |
The set type of the operands must be the same, or an error will be generated by the compiler.
The relational operators are listed in table ()
Operator | Action |
= | Equal |
<> | Not equal |
< | Stricty less than |
> | Strictly greater than |
<= | Less than or equal |
>= | Greater than or equal |
in | Element of |
Left and right operands must be of the same type. You can only mix integer and real types in relational expressions.
Comparing strings is done on the basis of their ASCII code representation.
When comparing pointers, the addresses to which they point are compared. This also is true for PChar type pointers. If you want to compare the strings the Pchar points to, you must use the StrComp function from the strings unit.
The in returns True if the left operand (which must have the same ordinal type as the set type) is an element of the set which is the right operand, otherwise it returns False