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"
}
}