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 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.

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.