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:
The flowchart for this type of problem will look something like this: