Nested conditionals and what are they?

  • Conditional Statements: A conditional statement in programming is a way to make decisions. It checks if something is true or false, and based on that, it tells the program what to do next.
  • Nested Conditionals: A nested conditional in coding is when an if or else statement is placed inside another if or else statement. This allows for checking multiple conditions in sequence

Flowchart for Nested If-Else Statements

Example #1: Drivers license

Python

age = int(input("Enter your age: "))
has_license = input("Do you have a driver's license? (yes/no): ").lower()

if age >= 16:
    if has_license == "yes":
        print("You can drive!")
    else:
        print("Get a driver's license.")
else:
    print("Not old enough to drive.")
Not old enough to drive.

Javascript

%%js
// Get user input
let age = parseInt(prompt("Enter your age: "));
let hasLicense = prompt("Do you have a driver's license? (yes/no): ").toLowerCase();

// Check if the person can drive
if (age >= 16) {
    if (hasLicense === "yes") {
        console.log("You can drive!");
    } else {
        console.log("Get a driver's license.");
    }
} else {
    console.log("Not old enough to drive.");
}
<IPython.core.display.Javascript object>
  • User Input: The code prompts the user to enter their age and whether they have a driver’s license, storing the values in age and hasLicense.
  • Age Check: It first checks if the user is 18 years old or older. If true, it proceeds to check if they have a driver’s license.
  • License Check: Depending on whether they have a license, the code outputs either “You can drive!” or “Get a driver’s license.” If the user is under 18, it outputs “Not old enough to drive.”

Example #2: Credit Score

Python

credit_score = 100
income = 10000

if credit_score < 700:
    print("Not eligible for loan due to low credit score")
elif income < 50000:
    print("Not eligible for loan due to low income")
else:
    print("Eligible for loan")
Not eligible for loan due to low credit score

Javascript

%%js
let credit_score = 100;
let income = 10000;

if (credit_score < 700) {
    console.log("Not eligible for loan due to low credit score");
} else if (income < 50000) {
    console.log("Not eligible for loan due to low income");
} else {
    console.log("Eligible for loan");
}
<IPython.core.display.Javascript object>
  • Both snippets (Python and JavaScript) check loan eligibility based on two factors: credit_score and income.
  • If the credit_score is less than 700, both codes print a message stating that the person is not eligible due to a low credit score.
  • If the credit_score is 700 or higher but the income is less than 50,000, both codes output that the person is not eligible due to low income.
  • If both conditions are met (credit score ≥ 700 and income ≥ 50,000), both snippets print that the person is eligible for a loan.
  • The Python code uses print() to display messages in the console, while the JavaScript code uses console.log() to achieve the same result.

Example #3 : Shipping

is_subscriber = True
order_weight = 12

if is_subscriber:
    if order_weight > 10:
        print("Free express shipping")
    else:
        print("Free standard shipping")
else:
    if order_weight > 10:
        print("Free standard shipping")
    else:
        print("No free shipping")

Free express shipping
  • The code checks if a person is a subscriber using the is_subscriber variable.
  • If they are a subscriber and the order_weight is greater than 10, it prints “Free express shipping.”
  • If they are a subscriber and the weight is 10 or less, it prints “Free standard shipping.”
  • If they are not a subscriber, it checks the weight again and either prints “Free standard shipping” or “No free shipping” based on the weight.

Summary of Nested Conditionals and Conditional Statements

  • Conditional Statements*: Conditional statements are programming structures that run specific code based on whether a condition is true or false. The most common type is the if statement, which can be followed by else or else if to manage different outcomes.
  • Nested Conditionals*: Nested conditionals are when one conditional statement is placed inside another, allowing you to check extra conditions based on earlier ones, which helps your program make more complex decisions.