问题
The If statement and any other boolean comparison is smart enought to stop at first FALSE value when evaluating A and B and C and D
and at first TRUE value when evaluating A or B or C or D
.
What is the name of this behavior?
Is this a compiler optimization? If so, there is a way to disable it with some compiler directive?
回答1:
This is called 'boolean short-circuit evaluation', a form of 'lazy evaluation'.
You can tell the compiler either to use or not to use this feature using compiler directives:
Complete evaluation Lazy evaluation
{$B+} {$B-}
{$BOOLEVAL ON} {$BOOLEVAL OFF}
But notice that this isn't only an optimisation, since this feature allows you to write code like
if (length(myarr) > 0) and (myarr[0] = MY_VAL) then
which will work even if myarr[0]
doesn't exist. This is rather common, actually.
来源:https://stackoverflow.com/questions/18598466/break-condition-to-or-and-and-operators-in-an-if-statement