Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › CONTROL FLOW
A switch compares its value with each case label using exact equality. For the calculator, choice selects one operation: 1 adds, 2 subtracts, 3 multiplies, and 4 divides. The labels are entry points, not steps that must be visited in written order.
int a = 18;
int b = 6;
int choice = 2;
switch (choice) {
case 1:
cout << a + b << '\n';
break;
case 2:
cout << a - b << '\n';
break;
case 3:
cout << a * b << '\n';
break;
case 4:
cout << a / b << '\n';
break;
default:
cout << "Invalid choice" << '\n';
}With choice equal to 2, the switch checks for a case label equal to 2 and enters case 2. It skips the addition body in case 1, prints 18 - 6, which is 12, and then reaches the break. The break sends execution to the point after the closing brace of the switch.
For the calculator with choice = 2, where does execution enter the switch?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Finding a matching case does not automatically end that case body. Once execution enters case 2, it continues through the statements below it until it finds break or reaches the end of the switch. This behavior is called fall-through.
switch (choice) {
case 1:
cout << a + b << '\n';
break;
case 2:
cout << a - b << '\n';
// break removed
case 3:
cout << a * b << '\n';
break;
case 4:
cout << a / b << '\n';
break;
default:
cout << "Invalid choice" << '\n';
}For choice equal to 2, execution enters case 2 and prints 12. Without the break there, it continues into case 3 and prints 108, then reaches the break in case 3 and exits. Case 4 is not reached in this version because case 3's break stops execution first.
If the break after case 3 were also absent, execution would continue into case 4 and print 3 before leaving the switch. Restoring the break immediately after the subtraction output makes the calculator print only 12 for choice equal to 2. The position of break matters because it is the instruction that changes execution from running a case body to leaving the entire switch.
The default body is the switch's fallback. It runs only when the switch value is not equal to any listed case label. For choice equal to 9, the value is compared with 1, 2, 3, and 4, but none matches, so execution enters default and prints "Invalid choice".
int choice = 9;
switch (choice) {
case 1:
cout << a + b << '\n';
break;
case 2:
cout << a - b << '\n';
break;
case 3:
cout << a * b << '\n';
break;
case 4:
cout << a / b << '\n';
break;
default:
cout << "Invalid choice" << '\n';
}Choice equal to 2 does not reach default when case 2 has a break. Exact equality selects case 2, its output runs, and break exits the switch before default can run. Default is not a final case that runs after every other case. It is selected only when no case label was selected.
Insert the missing statement after the subtraction output so choice = 2 does not continue into multiplication.
case 2:
cout << a - b << '\n';
______
case 3:Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
A reliable switch trace records three events: the label selected as the entry point, the bodies executed after entry, and the instruction that sends execution out. For choice equal to 2, case 2 is selected, the subtraction body runs, and its break exits. For choice equal to 9, no numbered label is selected, default runs, and execution then reaches the end of the switch.
| CHOICE | SELECTED ENTRY POINT | EXECUTED BODY | EXIT |
|---|---|---|---|
| 2 | case 2 | 18 - 6 prints 12 | break sends execution after the switch |
| 9 | default | prints "Invalid choice" | end of the switch sends execution after the switch |
A wrong case label changes the entry decision. If the subtraction branch is labeled case 20 instead of case 2, choice equal to 2 finds no matching numbered label and falls to default. A missing break changes the executed body after entry, so choice equal to 2 may print the subtraction result and then run multiplication or division. A missing default leaves choice equal to 9 with no fallback output, because no label matches it.