Interruption condition for OR and AND statements in IF statement

The If statement and any other logical comparison are smart enough to first stop the FALSE value when evaluating A and B and C and D and first the TRUE value when evaluating A or B or C or D

What is this behavior called?
Is this a compiler optimization? If so, is there a way to disable it using some kind of compiler directive?

+4
source share
1 answer

This is called a "logical short circuit assessment", a form of "lazy assessment".

You can tell the compiler to use or not use this function using the compiler directives :

 Complete evaluation Lazy evaluation {$B+} {$B-} {$BOOLEVAL ON} {$BOOLEVAL OFF} 

But note that this is not only optimization, as this function allows you to write code like

 if (length(myarr) > 0) and (myarr[0] = MY_VAL) then 

which will work even if myarr[0] does not exist. This is actually quite common.

+14
source

Source: https://habr.com/ru/post/1500355/


All Articles