简体   繁体   中英

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:

 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_valval进行比较temp_val未定义temp_val

The equality comparison operator in C is == . In Delphi the equivalent operator is = .

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. 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. And that returns false and so DoSomething() is never executed. I believe that this potential confusion is one of the reasons why Pascal chose to use := for assignment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM