• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

# Struggling with ==

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

12 replies to this topic

### #1stitchs  Members

Posted 10 July 2012 - 12:08 PM

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.

### #2Álvaro  Members

Posted 10 July 2012 - 12:13 PM

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
}

### #3Brother Bob  Moderators

Posted 10 July 2012 - 12:16 PM

It should be obvious if you rewrite it like this:

if(((a == b) == c) == d)



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.

### #4stitchs  Members

Posted 10 July 2012 - 12:28 PM

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

### #5Brother Bob  Moderators

Posted 10 July 2012 - 12:34 PM

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.

### #6Acotoz  Members

Posted 10 July 2012 - 01:43 PM

The AND (&&) operator is pure magic my friend, use it

Good luck

### #7stitchs  Members

Posted 10 July 2012 - 01:58 PM

Magic indeed, thank you for clarifying this.

### #8Narf the Mouse  Members

Posted 10 July 2012 - 02:07 PM

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

### #9stitchs  Members

Posted 11 July 2012 - 04:34 PM

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.

### #10Cornstalks  Members

Posted 11 July 2012 - 05:34 PM

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, 11 July 2012 - 05:37 PM.

[ 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 ]

### #11Krypt0n  Members

Posted 12 July 2012 - 02:55 AM

It should be obvious if you rewrite it like this:

if(((a == b) == c) == d)


also:
if(a == b)
{
if(1== c)
{
if(1== d)
{
...
}
}
}


### #12Álvaro  Members

Posted 12 July 2012 - 06:02 AM

It should be obvious if you rewrite it like this:

if(((a == b) == c) == d)


also:
if(a == b)
{
if(1== c)
{
if(1== d)
{
...
}
}
}


That's not right. If a=1, b=3, c=5, d=0 the original code would "do something", but your code would not.

### #13luca-deltodesco  Members

Posted 14 July 2012 - 12:18 PM

if you 'really' wanted to expand (if (a == b == c == d)) into cascaded if's you'd have to write:

[source lang="java"]if (a == b) { if (1 == c) { if (1 == d) { ... } }else if (0 == d) { ... }}else { if (0 == c) { if (1 == d) { ... } }else if (0 == d) { ... }}[/source]

with each ... being replaced by the body of the origin if statement

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.