Delphi ‘AND’ evaluation with 2 conditions

The documentation link is here:

Boolean short-circuit evaluation

Type Switch
Syntax {$B+} or {$B-} {$BOOLEVAL ON} or {$BOOLEVAL OFF}
Default {$B-} {$BOOLEVAL OFF}
Scope Local

The $B directive switches between the two different models of Delphi code generation for the and and or Boolean operators.

In the {$B+} state, the compiler generates code for complete Boolean expression evaluation.
This means that every operand of a Boolean expression built from the and and or operators is guaranteed to be evaluated, even when the result of the entire expression is already known.

In the {$B-} state, the compiler generates code for short-circuit Boolean expression evaluation, which means that evaluation stops as soon as the result of the entire expression becomes evident in left to right order of evaluation.

As you can see, the default option is for short-circuit evaluation.


Unfortunately you got a little mixed up in your test. Your Delphi code is in fact quite different from the C code.

if (somefunc() == FALSE && anotherfunc() == TRUE)       // C code
if (somefunc() = False and anotherfunc() = True) then   // Delphi code

In Delphi the and operator has a higher precedence than the equality operator =. Which means that your Delphi code is equivalent to:

if (somefunc() = (True and anotherfunc()) = True) then

But in C and C++, the precedence is the other way around. So && has lower precedence than ==. And so the Delphi and C++ if statements in your question are logically different, irrespective of short-circuit evaluation.

I’m quite sure that you really meant to write your Delphi code like this:

if ((somefunc() = False) and (anotherfunc() = True)) then

That would give the same logic as your C++ code, and you would have seen the same behaviour due to short circuit evaluation.

Finally, you should never test against False and True in Delphi. Always write the code like this:

if not somefunc() and anotherfunc() then 

Leave a Comment