Delphi 'AND' 评估有 2 个条件 [英] Delphi 'AND' evaluation with 2 conditions
问题描述
最近我正在做的一项合同工作中,我不得不使用 Delphi,我希望有人澄清的一件事是在条件语句中执行逻辑,例如 if代码>.
我有 C/C++ 和这些语言的背景,一旦知道 if
语句失败,其余的逻辑就不会执行.例如:
if (somefunc() == FALSE && anotherfunc() == TRUE)
在上面的例子中,如果 somefunc()
返回 TRUE
那么 anotherfunc()
永远不会被调用.
就我目前所见,在 Delphi 中,这是不正确的.相反,对于
if (somefunc() = False and anotherfunc() = True) then
那么,不管 somefunc()
返回什么,anotherfunc()
都会被调用.
我阅读了各种 Delphi 书籍,并重读了一些有条件的章节,但根本找不到任何地方提到这种行为.任何人都可以指出我在 Delphi 或 Pascal 中的某个地方说明了这种行为吗?
布尔短路评估
<前>类型开关语法 {$B+} 或 {$B-} {$BOOLEVAL ON} 或 {$BOOLEVAL OFF}默认 {$B-} {$BOOLEVAL OFF}范围本地$B 指令在两种不同的 Delphi 模型之间切换and 和 or 布尔运算符的代码生成.
在{$B+}状态下,编译器生成完整的布尔型代码表达式评估.这意味着布尔值的每个操作数从 and 和 or 运算符构建的表达式保证是评估,即使整个表达式的结果已经已知.
在{$B-}状态下,编译器生成短路的代码布尔表达式求值,这意味着求值停止为一旦整个表达式的结果在 left 中变得明显正确的评估顺序.
如您所见,默认选项是短路评估.
<小时>不幸的是,您在测试中有点混淆.您的 Delphi 代码实际上与 C 代码完全不同.
if (somefunc() == FALSE && anotherfunc() == TRUE)//C 代码if (somefunc() = False and anotherfunc() = True) then//Delphi 代码
在 Delphi 中,and
运算符具有更高的优先级 比等号运算符 =
.这意味着您的 Delphi 代码相当于:
if (somefunc() = (True and anotherfunc()) = True) then
但是在 C 和 C++ 中,优先级是相反的.所以&&
的优先级低于==代码>.因此,无论短路评估如何,您问题中的 Delphi 和 C++ if 语句在逻辑上都是不同的.
我很确定您真的打算像这样编写 Delphi 代码:
if ((somefunc() = False) and (anotherfunc() = True)) then
这将提供与您的 C++ 代码相同的逻辑,并且由于短路评估,您会看到相同的行为.
最后,永远不要在 Delphi 中针对 False
和 True
进行测试.总是这样写代码:
如果不是 somefunc() 和 anotherfunc() 那么
I've had to pick up Delphi for a recent contract piece of work I am doing and one of the things I'd like someone to clarify is the execution of logic in a conditional statement such as an if
.
I come from a background in C/C++ and in those languages, as soon as an if
statement is known to fail, the rest of the logic is not executed. For example:
if (somefunc() == FALSE && anotherfunc() == TRUE)
In the case above, if somefunc()
returns TRUE
then anotherfunc()
is never called.
In Delphi from what I can see so far, this does not hold true. Rather, for
if (somefunc() = False and anotherfunc() = True) then
then, irrespective of what somefunc()
returns, anotherfunc()
will be called.
I've read various Delphi books, and reread some of the conditional chapters and can't find mention of this behaviour anywhere at all. Can anyone point me to somewhere in Delphi or Pascal where this behaviour is stated?
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 LocalThe $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
这篇关于Delphi 'AND' 评估有 2 个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!