- Home /
Does This Make Sense? Is It Correct?
Hello, thank you for taking the time to read this. I just need to know if this code means what I think it means.
if (Blah1 == 1 && Blah2 > 2 || Blah1 == 1 && Blah2 == 1) {
// Do Something.
}
So I am hoping that this means "If Blah1 is equal to 1 and Blah2 is greater then 2 then do this code OR IF Blah 1 is equal to 1 and Blah2 is equal to 1 then do this code." Is this what this means?
Thanks in advance!
no it doesnt, what it means now is
"if Blah1 is equal to 1 and Blah2 is greater then 2 or Blah1 is qual to 1 and Blah 2 is equal to 1" then do something.
In your description what you are hoping for is
if((Blah1 == 1 && Blah2 > 2) || (Blah1 == 1 && Blah2 == 2))
{
// Do something
}
$$anonymous$$ewEight so the code you showed me is what I should be doing for it to mean what I was hoping for it to mean correct? If so, please re-post it as an answer so that I can accept it as correct, and reward you a couple reputation points. Thank you!
Answer by pudd1nG · Sep 03, 2015 at 08:36 AM
No, it wont. If I'm reading this correctly you're checking if Blah2 is 1 or higher than 2. You can do this instead.
if (Blah1 == 1 && Blah2 != 2)
If you really need those 2 specific checks, you'll want to encapsulate them in brackets.
if((Blah1 == 1 && Blah2 > 2) || (Blah1 == 1 && Blah2 == 2))
Hope that's helpful.
Hmm, I don't believe this is what I am looking for. Thanks for the quick reply pudd1ng!
It's what your code is doing, essentially. Since checking for 1 or greater than 2 is convoluted way to checking that the value is not 2.
If this needs to be that way because of other factors you can stick with my second recommendation which will work.
Hey thanks pudd1ng since that is pretty much what $$anonymous$$ewEight wrote, out of respect I would like to wait and see if he replies his comment as an answer. If by tomorrow afternoon he does not reply, I will accept your answer as correct. Hope that is ok with you. Again thanks for the help.
Depending on how you want it sorted, this can also be simplified or extended into multiple segments.
Because you're checking for "Blah1" to be equal to 1 in either case, you can do either:
if(Blah1 == 1 && (Blah2 == 1 || Blah2 > 2))
or
if(Blah1 == 1)
{
if(Blah2 == 1 || Blah2 > 2)
{
// Do something
}
}
The second version is based around the idea that "Blah1" won't always be equal to 1. If that's the case, then it doesn't matter what the state of "Blah2" is without the first case being true, so the whole thing will be skipped over with $$anonymous$$imal effort.
Hey Eno $$anonymous$$haon thanks for that comment. Its very informative and helped me understand this even better.
Answer by Baste · Sep 03, 2015 at 09:05 AM
Binary operations are defined by precedence rules - the operator that has higher precedence is resolved first. && has a higher precedence than ||, so in your case:
x && y || z && w
is the same thing as:
(x && y) || (z && w)
This works the same way with addition and multiplication:
1 * 2 + 3 * 4
is the same thing as:
(1 * 2) + (3 * 4)
This means that your code is doing what you think it's doing. On the other hand, though:
x || y && z || w
is the same thing as:
x || (y && z) || w
which is resolved left-to-right:
(x || (y && z)) || w
So what operators you're using influences what order things are resolved in.
As a general rule, though, don't rely on the precedence - put your own () in to make it clear what you are trying to do.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
If statement question 3 Answers
play an audio clip in an if statement 2 Answers
C# Check for boolean value with while loop in Update function 1 Answer