break condition to OR and AND operators in an IF Statement

佐手、 提交于 2021-02-20 17:03:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!