[英]How do I compare two numbers for equality in Delphi?
I'm converting a code from C to Delphi, but I'm stuck on the last line of this code: 我正在将代码从C转换为Delphi,但是我停留在代码的最后一行:
BOOL is_match = FALSE;
unsigned int temp_val;
unsigned int prev_val = 0;
is_match = (temp_val == val);
I can only convert this much: 我只能这么转换:
var
is_match: boolean;
temp_val: cardinal;
prev_val: cardinal;
begin
is_match := false;
prev_val := 0;
is_match := ????
end;
How do I fill in the last assignment? 如何填写上一个作业?
is_match := temp_val = val;
无论如何,我希望上面的代码只是真实代码的一小部分,因为在将temp_val
与val
进行比较temp_val
未定义temp_val
。
The equality comparison operator in C is ==
. C中的相等比较运算符为==
。 In Delphi the equivalent operator is =
. 在Delphi中,等效运算符为=
。
So you need to use this code: 因此,您需要使用以下代码:
is_match := temp_val=val;
Interestingly, as an aside, the C equality operator leads to a very classic and hard to spot bug. 有趣的是,顺便说一句,C相等运算符会导致一个非常经典且难以发现的错误。 It goes like this: 它是这样的:
if (x=0)
DoSomething();
What happens here is that =
is the assignment operator and so x
is assigned a value of 0
which is then truth tested. 这里发生的是=
是赋值运算符,因此x
被赋值为0
,然后进行真值检验。 And that returns false and so DoSomething()
is never executed. 并且返回false,因此从不执行DoSomething()
。 I believe that this potential confusion is one of the reasons why Pascal chose to use :=
for assignment. 我相信这种潜在的混乱是Pascal选择使用:=
进行分配的原因之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.