Next: 7. Statements
Up: 6. Expressions
Previous: 6.5 The @ operator
Subsections
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 (binaroperators) , unary operators are
listed in table (unaroperators) .
Table:
Binary arithmetic operators
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.
Table:
Unary arithmetic operators
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 :
I mod J = I - (I div J) * J
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 (logicoperations) .
Table:
Logical operators
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:
A shr 1 { same as A div 2, but faster}
Not 1 { equals -2 }
Not 0 { equals -1 }
Not -1 { equals 0 }
B shl 2 { same as B * 2 for integers }
1 or 2 { equals 3 }
3 xor 1 { equals 2 }
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 (booleanoperators)
Table:
Boolean operators
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:
B := True or MaybeTrue;
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:
'This is ' + 'VERY ' + 'easy !'
Dirname+'\'
The following is not:
Var Dirname = Pchar;
...
Dirname := Dirname+'\';
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 (setoperators) .
Table:
Set operators
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 (relationoperators)
Table:
Relational operators
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
root
1999-06-10