paulohr
paulohr

Reputation: 586

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?

Upvotes: 1

Views: 4683

Answers (2)

David Heffernan
David Heffernan

Reputation: 613612

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.

Upvotes: 7

Andreas Rejbrand
Andreas Rejbrand

Reputation: 109168

is_match := temp_val = val;

At any rate, I hope the code above is just a small excerpt of the real code, because temp_val is undefined at the time you compare it with val.

Upvotes: 12

Related Questions