Section 12.5 Putting It All Together
Using the order of precedence, you should now be able to evaluate statements with mathematical, relational, and logical operators all in tandem. The trick is to reduce the statement using the order of precedence until you are left with a single Boolean value.

Letβs consider one more example together to tie it all together:
the_answer = 3 == 3 & (~ (42 == 21 * 2 | 65-2 == 65 + 3))
Try It!
Before you move on to see my solution, make sure you try to solve it on your own! Remember, use the order of precedence, and reduce the statement until you are left with one Boolean value. You can do it! It is important to not skip your brain workouts!
Ok, now that you have your solution, I will show you how I would solve this problem:
Since parenthesis are first on the order of precedence, we will start with the innermost set and reduce this chunk first:
(42 == 21 * 2 | 65-2 == 65 + 3)
According to the order of precedence, we will want to perform the multiplication step first. I am also going to show you a shortcut.
( 42 == 42 | < some other relational operation > )
Why did I do the
21*2 but skip everything on the right side of the logical |? Remember, logical | is chill, if either of the statements being compared evaluates to true, the whole thing is true. Since 42 == 42 is true, it doesnβt matter what < some other relational operation > evaluates to! Returning to our original statement we have:
the_answer = 3 == 3 & (~ true)
The next step would be to evaluate the parenthesis. In this case
~true is false so we reduce it to:
the_answer = 3 == 3 & false
We are almost done! Here we could take another shortcut even though the
3 == 3 is easy. Remember, logical & is uptight. Since we have a false being compared with a logical &, it doesnβt matter what 3 == 3 evaluates to (hopefully it is obvious to your that 3 == 3 is true)! The whole thing is going to turn out false!
Disclaimer
The reality is that you will never have to solve problems like this in the real world. Instead, you will be asked to utilize relational and logical operators in tandem to create complicated programs that can react intelligently to different inputs. That is what our programs have lacked so far! The ability to make decisions. Hopefully, now you can see how combinations of these operators might allow us to control the flow of our programs in the future.
You have attempted of activities on this page.
