Fundamentals | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
3.7 Nested Conditionals
A supplemental blog on nested conditionals. Learn how nested conditionals allow for more complex decision-making by enabling multiple levels of conditions than regular conditionals.
Nested Conditionals
Introduction
**Nested conditionalsare a fundamental concept in programming where one conditional statement is placed inside another. This allows for more complex decision-making processes and enables programs to handle a wider range of scenarios.
Objectives
By the end of this lesson, students will be able to:
- Understand what nested conditionals are.
- Write pseudocode using nested conditionals.
- Apply nested conditionals to solve problems.
Key Concepts
Conditional Statements
: Used to perform different actions based on different conditions.Nested Conditionals
: A conditional statement inside another conditional statement.
Example Syntax (College Board Pseudocode)
College Board pseudocode uses the following structure for conditional statements:
IF condition1
{
// Code block for condition1
IF condition2
{
// Code block for condition2
}
ELSE
{
// Code block if condition2 is false
}
}
ELSE
{
// Code block if condition1 is false
}
`Example 1: Checking Grade Categories`
## Pseudocode
Let's write a pseudocode to determine the grade category based on a score:
```python
IF score >= 90
{
DISPLAY "A"
}
ELSE
{
IF score >= 80
{
DISPLAY "B"
}
ELSE
{
IF score >= 70
{
DISPLAY "C"
}
ELSE
{
IF score >= 60
{
DISPLAY "D"
}
ELSE
{
DISPLAY "F"
}
}
}
}
Python
Here is the equivalent Python code:
score = 85
if score >= 90:
print("A")
else:
if score >= 80:
print("B")
else:
if score >= 70:
print("C")
else:
if score >= 60:
print("D")
else:
print("F")
B
Example 2: Determining Eligibility for a Loan
Pseudocode
Let’s consider a scenario where we determine if a person is eligible for a loan based on their credit score and income:
IF credit_score >= 700
{
IF income >= 50000
{
DISPLAY "Eligible for loan"
}
ELSE
{
DISPLAY "Not eligible for loan due to low income"
}
}
ELSE
{
DISPLAY "Not eligible for loan due to low credit score"
}
Python
Here is the equivalent Python code:
credit_score = 750
income = 60000
if credit_score >= 700:
if income >= 50000:
print("Eligible for loan")
else:
print("Not eligible for loan due to low income")
else:
print("Not eligible for loan due to low credit score")
Example 3
Write pseudocode to determine if a person qualifies for a discount based on their membership status and purchase amount:
If the person is a member:
- If the purchase amount is greater than $100, they get a 20% discount.
- Otherwise, they get a 10% discount. If the person is not a member:
- If the purchase amount is greater than $100, they get a 5% discount. - Otherwise, they get no discount.
IF is_member = TRUE
{
IF purchase_amount > 100
{
DISPLAY "20% discount"
}
ELSE
{
DISPLAY "10% discount"
}
}
ELSE
{
IF purchase_amount > 100
{
DISPLAY "5% discount"
}
ELSE
{
DISPLAY "No discount"
}
}
Hacks
Review each of the sections above and produce …
- Write pseudocode to determine if a student passes a class based on their exam scores and attendance using nested conditionals.
- Write a python segment to decide the shipping cost based on the weight of a package and the delivery speed chosen (standard or express) using nested conditionals.