Making choices with if and switch
A conditional executes one path when its condition is true and another when it is false.
Learning objectives
- Choose between independent
ifstatements and anif/else if/elsechain. - Use
switchfor a set of discrete integral or enumeration values. - Trace a decision and predict which statements execute.
Prerequisites: variables, input/output, comparison operators, and Boolean values. Review Comparison and logical operators first.
How a condition controls a branch
An if statement evaluates its condition. If the result is true, its controlled statement runs; otherwise it is skipped. In an if/else if/else chain, conditions are checked in order and at most one branch is selected. Use braces to make each branch boundary clear.
#include <iostream>
int main() {
int marks{72};
if (marks >= 90) {
std::cout << "Excellent\n";
} else if (marks >= 40) {
std::cout << "Pass\n";
} else {
std::cout << "Try again\n";
}
}Trace the decision
| Check | Result for 72 | Next action |
|---|---|---|
marks >= 90 | false | Check the next condition |
marks >= 40 | true | Print Pass; skip later branches |
Conditions are contextual Boolean expressions: in C++, a scalar value can be contextually converted to bool (zero is false; nonzero is true). Prefer an explicit comparison when it communicates the rule more clearly.
When switch is a better fit
Use switch when one integral or enumeration expression is compared against several constant case labels. It is not a general replacement for ranges such as “marks at least 40”. A break exits the switch; without it, execution can fall through into the next case. A default label handles values not matched by another case.
#include <iostream>
int main() {
int choice{2};
switch (choice) {
case 1:
std::cout << "Start\n";
break;
case 2:
std::cout << "Settings\n";
break;
case 3:
std::cout << "Quit\n";
break;
default:
std::cout << "Unknown option\n";
break;
}
}Common mistakes
- Use
==for equality;=assigns a value. - Order range checks from the most specific/highest threshold when later branches depend on earlier ones.
- Remember that a missing
breakcan cause switch fall-through. - Do not expect
switchto accept arbitrary run-time ranges or strings as case labels in C++17.
Practice
- For
marks = 39, which branch runs in the first program? - For
choice = 7, what does the switch program print? - Why is an
if/else ifchain more suitable thanswitchfor classifying a numeric range? - What happens if the
breakaftercase 2is removed?
Worked answers
Show reasoned answers
- The final
elseprintsTry again. - The
defaultbranch printsUnknown option. - Range conditions such as
marks >= 40are Boolean expressions, not constant case labels. - Execution falls through to the next case body unless another control transfer exits the switch.
Summary
Use if for Boolean conditions and ranges; use switch for discrete integral or enumeration choices. Trace conditions in order and make branch boundaries explicit with braces and deliberate break statements.