Quick start manual
4-8
Delphi Language Guide
Expressions
•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 form x/y, x div y, or
x 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.
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 short-circuit Boolean evaluation
The compiler supports two modes of evaluation for the and and or operators:
complete evaluation and short-circuit (partial) evaluation. Complete evaluation means
that each conjunct or disjunct is evaluated, even when the result of the entire
expression is already determined. Short-circuit evaluation means strict left-to-right
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 short-circuit mode when A is
False, the compiler won’t evaluate B; it knows that the entire expression is False as
soon as it evaluates A.
Short-circuit 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.
Short-circuit 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 a 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 short-circuit evaluation, in contrast, the second part of the while condition—
(S[I] <> ',')—is not evaluated after the first part fails.
Table 4.6 Boolean operators
Operator Operation Operand types Result type Example
not negation Boolean Boolean not (C in MySet)
and conjunction Boolean Boolean Done and (Total > 0)
or disjunction Boolean Boolean A or B
xor exclusive disjunction Boolean Boolean A xor B