Introduction
Nested conditionals are 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
- 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
Javascript
Here is the equivalent Javascript code example:
let score = 85;
if (score >= 90) {
console.log("A");
} else if (score >= 80) {
console.log("B");
} else if (score >= 70) {
console.log("C");
} else if (score >= 60) {
console.log("D");
} else {
console.log("F");
}
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")
Javascript
Here is the equivalent Javascript code:
let credit_score = 750;
let income = 60000;
if (credit_score >= 700) {
if (income >= 50000) {
console.log("Eligible for loan");
} else {
console.log("Not eligible for loan due to low income");
}
} else {
console.log("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"
}
}
Quiz!
Homework (Hacks)
- 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.
- Write a python segment to have different ticket prices for different ages, with a discount for students
Challenge Hack
Write a program that helps a user determine the type of triangle based on the lengths of its sides. The program should prompt the user to input three positive numbers representing the sides of a triangle. Your task is to use nested conditionals to check the following:
First, verify if the three sides can form a valid triangle (hint: the sum of any two sides must be greater than the third side). If it’s a valid triangle, further classify it into one of the following categories:
- Equilateral Triangle: All sides are equal.
- Isosceles Triangle: Two sides are equal.
- Scalene Triangle: No sides are equal. If the sides do not form a valid triangle, the program should display an appropriate message.
Requirements: Use nested conditionals to handle the logic for checking the type of triangle. Include input validation to ensure the user enters positive numbers.
Need Help?
Here’s some solutions.