Boolean Evaluation


Delphi supports two modes of Boolean evaluation: complete and short-circuit. The default evaluation mode is the short-circuit mode since it results in faster code execution and smaller code size. The Boolean evaluation mode only alters the behavior of the Boolean or and Boolean and operators.

x := 8; if (x > 10) and (x < 20) then begin end;

In this example, the if-then statement tests whether the number x is in the 11 to 19 range. If we use the short-circuit evaluation mode, only the x > 10 part of the statement is evaluated. Since x > 10 evaluates to False, there is no need to evaluate the statement any further.

In complete evaluation mode, the entire statement is evaluated, regardless of the fact that the result is known immediately after the first part of the statement is evaluated. In this case, complete evaluation is absolutely unnecessary, but there are situations where short-circuit evaluation can result in random and erroneous behavior. (You can read more about this in Chapter 5 in the "Creating Functions" section.)

You can change the Boolean evaluation mode for specific code sections by using the $B or $BOOLEVAL compiler directives. To switch to complete Boolean evaluation, use the $B+ or $BOOLEVAL ON directives. Use $B- or $BOOLEVAL OFF to switch to short-circuit evaluation.

You can also change the Boolean evaluation mode for the entire project in the Project Options dialog box.

image from book
Figure 3-4: Boolean evaluation option



Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net