Slide 1 / 15
Nested if — decisions inside decisions
A nested if is simply an if statement placed inside another if statement. It allows you to make a decision, and then based on that result, make another decision.
Think of it like: "If it's raining, and if I have an umbrella, I'll go out."
code_example.cpp
int num1, num2, num3;
cin >> num1 >> num2 >> num3;
// Find the largest of three numbers
if (num1 > num2) {
// num1 is bigger than num2, now compare with num3
if (num1 > num3)
cout << num1 << " is the largest";
else
cout << num3 << " is the largest";
} else {
// num2 is bigger than num1, now compare with num3
if (num2 > num3)
cout << num2 << " is the largest";
else
cout << num3 << " is the largest";
}