Having trouble with the equality operator: '=='

Hi Folks,

Stuck again.

I have a variable named TI that has the value 13, that refuses being compared.

int TI;

Serial.print("’");
Serial.print(TI);
Serial.println("’");

// Complete message. (13 tokens.)
if (TI != 13) {
Serial.println(“Huh?”);
Serial.println((TI == 13));
return false;
}

The code returns.
‘13’
Huh?
0

(13 == 13) evaluates as true, but not (TI == 13) evaluates as false.

Any ideas?

Please post a complete program that illustrates the problem

The code you posted says "if (TI != 13)". That's not equal. Then you print (TI == 13), the result of comparing it to 13. So it prints 0 which equals false. Which you should already know because if it is 13 it skips the print.

Steve

UKHeliBob: This IS the relevant part that doesn't work!

slipstick: Nice try, but you miss the issue.

Serial.print prints 13. That is TI = 13.

The if statement treats TI != 13 as true, which is not the case. The if code block should never be entered.

TI == 13 should be true, but it's false.

TI seems to be the issue but I can't see why.

The purpose of the if is to exit the routine if TI is not 13, TI must be 13 to continue.

UKHeliBob: This IS the relevant part that doesn't work!

But it is not a complete program that would allow us to try it for ourselves

What type variable is TI?

UKHeliBob:
But it is not a complete program that would allow us to try it for ourselves

@OzMaz, when you finally post a complete program, do so using Code Tags as described in Item #6 here: Read this before posting a programming question .... Despite the rather obvious title, you appear to have missed that post.

The problem must indeed be elsewhere. When I try a program using the snippet you posted, I get the correct result:

void setup() {
  int TI = 13;
  Serial.begin(115200);
  while (!Serial);
  Serial.print("**");
  Serial.print(TI);
  Serial.println("**");

  // Complete message. (13 tokens.)
  if (TI != 13) {
    Serial.println("Huh?");
    Serial.println((TI == 13));
  }
}

void loop() {
}

Displays only:

13

So the if condition provides correct result and no ‘Huh?’

Big apology folks, I got it wrong.

I wrote more items than was assigned to the Buffer and boom, buffer overrun, crazy results.

Well, at least you came clean...