Section 12.4 Logical Operator
The last thing we need to learn before we can start having our programs make decisions is how to combine multiple relational operators together. Although it is jumping a little ahead, letβs consider why we need the logical operators.
Consider designing a piece of software that needs to make a decision about a range. For example, I personally ride my bike to work but wonβt ride if the forecasted high is above \(35C\) or if the forecasted low is below \(-10C\text{.}\) If I wanted to make an app that could tell me if I should ride my bike to work or not, I would need a program that could determine if: the forecast high is less than \(35C\) AND forecast low is greater than \(-10C\text{.}\) The AND in the previous statement is formatted in code because it represents where we would have included a
logical and.

The logical operators give us a way to string together multiple relational statements and get one true or false out. When looking at the order of precedence you may have noticed the three logical operators, shown in figure 12.4.2 below. Take a minute to look at figure 12.4.2 below and read the description of each of the logical operators.

You can see that the logical operators
& and | allow us to string together multiple relational operations! Cool! Consider the following MATLAB command:
6 >= 7 & 5 >= 6
According to the order or precedence, we evaluate the relational operators first. That means that the statement reduces to:
false & true
Looking up in figure 12.4.2 for the correct way to use the logical operator, we can see that
false & true evaluates to false!
Logical
& is the type A logical operator. It needs all the relational operators to evaluate to true in order to return true. If logical & was a person they would be kind of uptight but would probably be very successful. Only the truest of the true for logical &!
Logical
| is the cool guy logical operator. If any of the relational operators evaluate to true, it calls the whole thing true. Logical | probably drives a Volkswagen bus and says βradβ a lot. All logical | cares about is at least one truth.
In the future, we will see how both
& and | are critical to creating complex programs. Just like yin and yang, we need both & and | to make our programs work. Logical & and logical | are a little tricky but once you understand the logic, you will be able to make some cool programs.
A Short Note on Logical Not
~
Logical
~ is a little weird because I find it redundant and unnecessary. Why? If you have a statement like:
~(4>5)
We can see that the
(4>5) will evaluate as false. Then ~false is true. Ok, but why not just put 4 < 5? See what I am getting at? If you just reverse your relational statement, you shouldnβt need to flip your Boolean value. There might be a good reason that I have not yet run into for using a logical not. If you run into a good and necessary use of logical not, please email me and let me know ([email protected])!
You have attempted of activities on this page.
