In a previous blog I gave you a super crazy if statement to explain how to evaluate complex conditionals. To organize this, we used parenthesis. Let’s go over a simplified example.
In this situation, we do not have parenthesis to explain in what order these are going to be evaluated.
To explain what I mean, if || is evaluated before &&, we have this:
which is always true and would run all of the time. The compiler would probably remove the conditional all together and just leave the code inside of the code block because it’s going to be ran always anyways.
If && is evaluated before ||, we have:
which is just z (see the short circuit evaluation video).
As you can see, the result of our code is completely different depending on how this works.
Well, I’ll tell you, that the precedence is first !, then &&, and then ||. This means that the previous expression would be evaluated as true || (y && z).
If we have a negation, it happens first. So if we had
First, the truth value of 1 will be negated, so we have:
then, the ands are evaluated. Let’s say that both y and z evaluate to true. we now have
Which is always true.
We should try to always use parenthesis to be crystal clear what we are trying to do. For example, if we wanted to negate the entire thing rather than just the first operand, we can do this:
Which is always false.
Are you ready to read the next blog? Let’s go!