Nested If Statements¶
Sometimes a single if statement, even a long If…Then…ElseIf…ElseIf…Else is not a suitable structure to model your problem. Sometimes after one decision is made, there is another second decision that must follow. In these cases, if statements can be nested within if statements (or other structures as we will see later). Here is a problem:
The nested if statements (in most computer programming languages) takes the generic form of:
A school is going to sell chocolate bars to raise money. If a student sells 20 or more boxes, they get a prize. If they sell less than 30, they get a “small” prize. If they sell more than 30, they get a “large” prize. (Yes you could use an If…Then…ElseIf… statement.)
An example of what this would look like in a specific programming language is:
// Copyright (c) 2019 St. Mother Teresa HS All rights reserved.
//
// Created by: Mr. Coxall
// Created on: Sep 2019
// This program uses a nested if statement
#include <iostream>
#include <string>
main() {
// this function uses a nested if statement
int cookiesSold;
// input
std::cout << "Enter the number of boxes of cookies you sold: ";
std::cin >> cookiesSold;
// process & output
if (cookiesSold >= 20) {
if (cookiesSold < 30) {
std::cout << "You get a small prize";
} else {
std::cout << "You get a large prize";
}
} else{
std::cout << "No prize";
}
}
// nested if example
// nested if example
// nested if example
#!/usr/bin/env python3
# Created by: Mr. Coxall
# Created on: Sep 2019
# This program uses a nested if statement
def main():
# this function uses a nested if statement
# input
cookies_sold = int(input("Enter the number of boxes of cookies you sold: "))
print("")
# process & output
if cookies_sold >= 20:
if cookies_sold < 30:
print("You get a small prize")
else:
print("You get a large prize")
else:
print("No prize")
if __name__ == "__main__":
main()
// nested if example
// nested if example
The flowchart for this type of problem will look something like this:
