有两个条件的Delphi'AND'评估 [英] Delphi 'AND' evaluation with 2 conditions

查看:104
本文介绍了有两个条件的Delphi'AND'评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须拿起Delphi来完成我最近的一项合同工作,我希望有人澄清的一件事是在条件语句中执行逻辑,例如 if

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.

我来自C / C ++和这些语言的背景,而已知语句失败,其余逻辑未执行。例如:

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)

在上述情况下,如果 somefunc()返回 TRUE ,则永远不会调用 anotherfunc()

In the case above, if somefunc() returns TRUE then anotherfunc() is never called.

在Delphi中从目前为止我所看到的,这并不成立。相反,对于

In Delphi from what I can see so far, this does not hold true. Rather, for

if (somefunc() = False and anotherfunc() = True) then

然后,无论 somefunc()返回什么,会被调用anotherfunc()

then, irrespective of what somefunc() returns, anotherfunc() will be called.

我已经阅读了许多Delphi书籍,并重新阅读了一些有条件的章节,但找不到提及这种行为的任何地方。有人可以指出我在Delphi或Pascal中声明这种行为的地方吗?

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?

推荐答案

文档链接位于此处


布尔短路评估



Boolean short-circuit evaluation

 
Type    Switch 
Syntax  {$B+} or {$B-} {$BOOLEVAL ON} or {$BOOLEVAL OFF} 
Default {$B-} {$BOOLEVAL OFF} 
Scope   Local 

$ B指令在两个不同的Delphi
代码生成模型之间切换布尔运算符。

The $B directive switches between the two different models of Delphi code generation for the and and or Boolean operators.

在{$ B +}状态下,编译器生成用于完整布尔
表达式求值的代码。这意味着,即使已经知道整个表达式的结果
,也可以保证对通过and和or运算符构建的布尔
表达式的每个操作数进行
评估。

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.

在{$ B-}状态下,编译器生成用于短路
布尔表达式求值的代码,这意味着求值后立即以
停止求值整个表达式的显示顺序从左到右依次为

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.

如您所见,默认选项为

不幸的是,您的测试有些混乱。您的Delphi代码实际上与C代码完全不同。

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

在Delphi中,运算符具有比同等运算符 = 更高的优先级。这意味着您的Delphi代码等效于:

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

但是在C和C ++中,优先级相反。因此,& c具有的优先级低于 == 。因此,无论短路评估如何,问题中的Delphi和C ++ if语句在逻辑上都是不同的。

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.

我很确定您确实打算编写Delphi像这样的代码:

I'm quite sure that you really meant to write your Delphi code like this:

if ((somefunc() = False) and (anotherfunc() = True)) then 

这将提供与您的C ++代码相同的逻辑,并且由于短路评估,您将看到相同的行为。

That would give the same logic as your C++ code, and you would have seen the same behaviour due to short circuit evaluation.

最后,永远不要对 False True 在Delphi中。始终这样编写代码:

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'评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆