Struggling with ==

This topic is 2168 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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.

Share on other sites
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 }

Share on other sites
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.

Share on other sites
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

Share on other sites
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.

Share on other sites
The AND (&&) operator is pure magic my friend, use it

Good luck

Share on other sites
Magic indeed, thank you for clarifying this.

Share on other sites
In short, what people are saying is that you're comparing (after the first comparison) booleans and chars. Not gonna work, in most languages.

Share on other sites
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.

Share on other sites
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. Edited by Cornstalks

• 17
• 11
• 15
• 9
• 49
• Forum Statistics

• Total Topics
631393
• Total Posts
2999766
×