Delphi Language Guide Delphi for Microsoft Win32 Delphi for the Microsoft. Net framework



Yüklə 0,84 Mb.
Pdf görüntüsü
səhifə64/294
tarix02.01.2022
ölçüsü0,84 Mb.
#41395
1   ...   60   61   62   63   64   65   66   67   ...   294
DelphiLanguageGuide

Boolean Operators
The Boolean operators not, and, or, and xor take operands of any Boolean type and return a value of type Boolean.
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
49


xor
exclusive disjunction Boolean
Boolean
A xor B
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 the 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.
Use the 
$B
 compiler directive to control evaluation mode. The default state is 
{$B}
, which enables short-circuit
evaluation. To enable complete evaluation locally, add the 
{$B+}
 directive to your code. You can also switch to
complete evaluation on a project-wide basis by selecting Complete Boolean Evaluation in the Compiler
Options dialog (all source units will need to be recompiled).
Note:
If either operand involves a Variant, the compiler always performs complete evaluation (even in the 
{$B}
state).

Yüklə 0,84 Mb.

Dostları ilə paylaş:
1   ...   60   61   62   63   64   65   66   67   ...   294




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin