next up previous contents index
Next: Statements Up: Expressions Previous: The @ operator

Operators

Operators can be classified according to the type of expression they operate on. We will discuss them type by type.

Arithmetic operators

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 (gif), unary operators are listed in table (gif).

  

Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
Div Integer division
Mod Remainder
Table: Binary arithmetic operators

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
Table: Unary arithmetic operators

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 :
listing2893
but it executes faster than the right hand side expression.

Logical operators

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 (gif).

  

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
Table: Logical operators

The following are valid logical expressions:


listing2908

Boolean operators

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 (gif)

  

Operator Operation
not logical negation (unary)
and logical and
or logical or
xor logical xor
Table: Boolean operators

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:
listing2930
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)

String operators

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:
listing2938
The following is not:
listing2940
Because Dirname is a null-terminated string.

Set operators

The following operations on sets can be performed with operators: Union, difference and intersection. The operators needed for this are listed in table (gif).

  

Operator Action
+ Union
- Difference
* Intersection
Table: Set operators

The set type of the operands must be the same, or an error will be generated by the compiler.

Relational operators

The relational operators are listed in table (gif)

  

Operator Action
= Equal
<> Not equal
< Stricty less than
> Strictly greater than
<= Less than or equal
>= Greater than or equal
in Element of
Table: Relational operators

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


next up previous contents index
Next: Statements Up: Expressions Previous: The @ operator

Michael Van Canneyt
Fri Sep 25 09:15:40 MEST 1998