Lesson 6
Nested If and Logical Operators
Author: Brandon Fajardo (9/1/17). Updated (8/6/19)
Introduction
Review: In the last lesson, you learned how to use if statement bodies with their conditions. You learned about increment and decrement which are used to change a value by 1. Finally, you learned about compound-assignment operators which are used to modify the value of a variable.
Lesson topic: This lesson will cover nested if statements and logical operators.
Topic 1 - Nested If Statements
Nested if statement (chained if statement): an if statement in the body of another if statement.
Notation:
Nested if statements allow you to have multiple conditions before the body of the if statement.
Having multiple conditions is useful if there are multiple ways you wish to restrict your if statement.
EX: For example, we use a nested if statements to restrict this equation to both the values of 2 and 5 to avoid division by 0.
For this purpose, a nested if statement serves us well. But we can actually use logical operators to serve us better.
Topic 2 - Logical Operators
In cases where multiple conditions are desired, logical operators can be used instead of nested if statements.
Logical operators allow us to condense a nested if statement into a single if statement.
Subtopic 1 - And
The “and” logical operator (&&) checks if all conditions being compared are true. Only if all the conditions are true will the code run. If any condition is false, the if statement condition will not be satisfied, and the code will not run.
Notation:
EX: In this example, the door must be both closed and locked for you to be safe inside. This makes sense, because in real life, both must be true at any time for you to be safe behind a door.
*This if statement condition is true because both conditions are true.
EX: In this example, I want my app to display a notification only if certain conditions are met.
*In this case, no notification will be displayed. Despite the other conditions being true, notifications are muted. Therefore, the code will not run.
EX: In this example, c must be between 0 and 10 (inclusive). This shows off another way that domain/range can be restricted by using logical operators.
Subtopic 2 - Or
The “or” logical operator (||) checks if any of the conditions being compared are true. If any condition is true, if statement condition is satisfied and the code will run.
Notation:
EX: In this example, enjoying any of these fruits as your favorite snack will make you healthier. Only one of these conditions has to be true.
*In this case, your favorite snack is oranges, but not peaches or kiwi. You still become healthier.
Problem #1: Logical Operators
Look back to the original nested if statement we wrote:
Try to rewrite it using logical operators instead.
*Solution at the bottom of the document
Subtopic 3 - Not
The not (!) logical operator is unique to the others (&& or ||). It does not combine conditions like the previous two.
Usually, a condition is satisfied when it is true. Instead, not (!) is used to test when a condition is false. The not operator is placed before the condition being inverted. The condition may need to be placed within a set of parentheses first.
EX: In this example, I want to award every player who did not come in first with a participation trophy. I came in second place.
First, the program sees that the condition position == 1 is false. The not (!) logical operator inverts this, and considers it true.
The code runs, and I get a participation trophy.
Notice: The not (!) logical operator is placed outside a pair of parentheses that surround the condition.
Problem #2: If Statement Word Problem
Sarah wants to buy a new notebook. She doesn’t mind what kind of notebook it is, so long as it’s not green. Sarah thinks green is gross. Can you write an if statement for her that protects her from buying green notebooks?
*Solutions at the bottom of this document
Subtopic 4 - Abusing Not
If two not (!) operators are used simultaneously, they cancel out. Therefore, using more than one not (!) operator for the same condition is redundant.
EX: In this example, the two not operators cancel out, ultimately making the condition true. The print statement runs.
Notice:
Sometimes, code using the not (!) logical operator can become confusing for others to understand.
At different times, utilizing not (!) can make your code easier to understand.
Try to use it for the latter and not the former.
Conclusion
Recap: In this lesson, you should have become comfortable with nested if statements and logical operators.
Next: In the next lesson, you will learn else statements, else-if statements, and ternary operators.
Solutions
Problem #1:
This example can be rewritten using the and (&&) logical operator.
Notice that our original nested if statement now looks verbose in comparison.
Problem #2:
Notice how efficient it was to simply say “not” green. Imagine having to define every single other color she is allowed to buy instead. This is arguably impossible due to the infinite spectrum of colors.
Last updated