Intro .1 .2 .3 .4 .5 .6 .7 .8 .9 .10 .11 .12 .13 .14 .15 .16 .17 .18

Nested Conditionals

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

By the end of this lesson, students will be able to:

  1. Understand what nested conditionals are.
  2. Write pseudocode using nested conditionals.
  3. Apply nested conditionals to solve problems.

Key Concepts

  1. Conditional Statements: Used to perform different actions based on different conditions.
  2. 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
}


<span style="color:blue">Example 1: Checking Grade Categories</span>

## 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.