IF语句中OR和AND运算符的中断条件 [英] break condition to OR and AND operators in an IF Statement
问题描述
If语句和任何其他布尔比较很聪明,足以在评估 A和B以及C和D
时停止在第一个FALSE值,而在评估 A或B或第一个TRUE值时停止C或D
.
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?
推荐答案
这称为布尔短路评估",是惰性评估"的一种形式.
This is called 'boolean short-circuit evaluation', a form of 'lazy evaluation'.
您可以使用但是请注意,这并不是唯一的优化,因为此功能使您可以编写类似
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
即使 myarr [0]
不存在也可以使用.实际上,这很普遍.
which will work even if myarr[0]
doesn't exist. This is rather common, actually.
这篇关于IF语句中OR和AND运算符的中断条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!