Struggling with ==

Started by
11 comments, last by luca-deltodesco 11 years, 9 months ago
Hello all,

Might be a basic question with a blindingly obvious answer, but I am having a slight issue.

I want to perform a conditional check on 4 variables, with the first three being equal too the last.

I thought I would go with

[source lang="cpp"]if(a == b == c == d){
// execute whatever
}[/source]
Okay, so I assumed that this would work, each variable represents a char type data. I have commented out b and c, so that there is a check to see if a == d, and this fires the execution block of code. But when I try to add b and c back in, one at a time, the conditional check fails. I have tried bracketing with c==d on the inner-most brackets. I think my brain has frazzled.

Thanks in advance for any assistance.

Stitchs.
Advertisement
Operator == takes two arguments and returns a bool, which means that a == b == c will do something like testing whether a is equal to b, and then checking whether the truth value of that comparison (interpreted as 1 for true and 0 for false) is equal to c. Most definitely not what you want.

This is probably what you want:
if (a == b && b == c && c == d) {
// execute whatever
}
It should be obvious if you rewrite it like this:
[source]
if(((a == b) == c) == d)
[/source]
The result of (a==b) is used as the left hand side of the comparison with c. The result of the equality operator is a boolean true or false, which is converted to 0 or 1 for subsequent comparsons.
I think that is what I want though, I am happy if A equals B equals C equals TestValue (d). I did try Brother Bob's way before, but it seems to no avail. Or is what you guys are saying that this is incorrect and if I would like to test all the values against TestValue (d), then I should roll with logical && and go a == Test && b == Test etc.

Thanks
What I said is not how to do it, I tried to explain why your way didn't work. You need to check them pairwise with logical and to ensure that they are all equal.
The AND (&&) operator is pure magic my friend, use it

Good luck
Magic indeed, thank you for clarifying this.
In short, what people are saying is that you're comparing (after the first comparison) booleans and chars. Not gonna work, in most languages.
Okay, now I get fully understand what you're all saying.

Basically, after the first comparison, which gets evaluated to 0 or 1 (depending on the condition), this may never equal the next character variable as said variable could represent a higher number/letter.
I like pictures. I know you're understanding, but here's a little drawing of what's happening:



if (a == b == c == d) // Step 1 for the computer (let's assume a does indeed equal b here)
^^^^^^ resolves to true
if (true == c == d) // Step 2 for the computer
^^^^^^ since a == b is true, replace it with true
if (true == c == d) // Step 3 for the computer
^^^^^^^^^ Now we have to check if true == c... Wait... what? true is a boolean, c is a char, and we're trying to check if they're equal to each other? This is where everything goes wrong...


I'm not even going to draw out the rest of what the computer does, because at this point you should see the logic is quite wrong.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

This topic is closed to new replies.

Advertisement