Fall 2024 - P1
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
3.7 Mastering Nested Conditionals - Unlocking Advanced Decision-Making in Programming
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.
Introduction
Nested conditionals are conditional statements (like if, else if, and else) placed within another conditional statement. They allow you to make more complex decisions in your programs by evaluating multiple conditions based on the outcomes of other conditions.
Example in Natural Language
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.
- Pseudocode: Pseudocode is like a way to plan out a program before you write the actual code. It uses simple, everyday language to describe the steps a computer should take.
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
}
# Explanation
<li><strong>Outer IF Statement:</strong> This checks the first condition.</li> <ul>
<li>If condition1 is true, the code inside the first set of curly braces { } runs.</li>
<li>If condition1 is false, the code inside the ELSE block runs.</li>
</ul>
<li><strong>Inner IF Statement:</strong> Inside the first code block (which runs if condition1 is true), there's another IF statement that checks condition2.</li> <ul>
<li>If condition2 is true, the code inside this second set of curly braces runs.</li>
<li>If condition2 is false, the code inside the ELSE block associated with condition2 runs.</li> </ul>
`Example 1: Checking Grade Categories`
## Pseudocode
What is pseudocode?
Pseudocode is a description designed for human reading rather than machine reading. Pseudocode is written in plain language, making it easy for anyone to understand the logic, regardless of their programming background.
Key Points for Writing Pseudocode:
- Use simple language that describes the logic clearly.
- Indent nested conditions for better readability.
- Use keywords like BEGIN, END, IF, ELSE, and PRINT to denote structure.
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.
Submission
Submit your assignment HERE
This is how we grade:
- 1.0 = above and beyond - mortensen level code
- 0.95 = challenge hack completed + all other hacks are well done
- 0.9 = all hacks completed, but challenge is not attempted/not good
- 0.85 = hacks are not well made / incomplete
- 0.75 = some hacks are missing
- 0.5 = not submitted
Need Help?
Here’s some solutions.