delphi - 整数的数学 'not'

标签 delphi

为什么delphi在while do循环中执行整数的数学“非”而不是强制转换为 bool 值? 示例

var myint:integer;
...
myint:=1;  
while not myint=5 do
begin
  myint:=myint+1;
  showmessage('myint now is : '+inttostr(myint));
end;    

最佳答案

您的表达式使用两个运算符:not= 。为了了解它是如何解析的,您需要查阅 operator precedence 的表格。 。

Operators    Precedence
----------------------------
@            first (highest)
not
----------------------------
*            second
/
div
mod
and
shl
shr
as
----------------------------
+            third
-
or
xor
----------------------------
=            fourth (lowest)
<>
<
>
<=
>=
in
is
----------------------------

This shows that not has the highest precedence of all operators, and is of higher precedence than =. Which means that your expression is parsed as if it were:

(not myint) = 5

在此表达式中,因为它绑定(bind)到整型变量,not双向否定

为了获得您想要的结果,您必须使用括号来表明您希望在 not 之前执行相等测试。 :

not (myint = 5)

现在,在这个表达式中,(myint = 5)是一个逻辑表达式,因此 not运算符现在是逻辑否定。

这种性质的问题的答案总是可以在 operator precedence 的表中找到。熟悉这张表将会带来好处。该表将告诉您如何解析省略括号的任何表达式。

最后,正如 Rudy 指出的那样,使用 <> 可以最清晰地编写您的具体表达式。运算符:

myint <> 5

关于delphi - 整数的数学 'not',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22293371/

相关文章:

delphi - 有没有办法保存对象的状态以便以后更快地重新加载?

multithreading - 阻止STA线程中的调用(等待,com调用)

delphi - 在 Delphi 中使自定义拖动图像不透明

multithreading - 多个连续线程上的 TThread.WaitFor

delphi - 如何声明 TDictionary 枚举器?

delphi - 在评估/修改 (Ctrl+F7) 窗口中调用辅助函数?

delphi - 生成 Delphi XML 文档

windows - 检查特定过程在delphi中是否升高

delphi - 如何正确地将C与Delphi静态链接?

delphi - 对象的字节大小,包括成员的大小