Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
A switch compares one expression with several fixed case labels. The expression is menuChoice, each case label is one allowed menu value, and each case body contains the output for that value. With menuChoice equal to 2, case 2 matches. Case 1 does not match, so its body is skipped. The default body is available only when no case label matches.
int menuChoice = 2;
switch (menuChoice) {
case 1:
cout << "Start game";
break;
case 2:
cout << "View profile";
case 3:
cout << "Settings";
break;
default:
cout << "Invalid choice";
break;
}The switch expression is evaluated once, then execution looks for an equal case label. For menuChoice equal to 2, execution passes over case 1 and begins at case 2. The label does not run an equality comparison as a separate statement in your code, but it represents the fixed value that the switch matches.
For menuChoice = 2, where does execution begin in this switch?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
After execution enters case 2, the switch does not automatically stop at the end of that case body. In the initial menu, case 2 prints "View profile" and then reaches case 3 because no break appears between them. Case 3 prints "Settings", and its break finally exits the switch.
menuChoice = 2
case 1: skipped
case 2: matched, print "View profile"
missing break
case 3: crossed without a new match, print "Settings"
case 3 break: exit switchThe output therefore has two lines, "View profile" and "Settings". The second line does not mean that menuChoice also matched 3. Once execution has entered a case, it continues through following case bodies until it reaches break or the end of the switch. This behavior is called fall-through.
Put break immediately after the output in case 2. When menuChoice is 2, that break exits the switch before execution reaches case 3. The switch prints only "View profile", and neither the Settings output nor the default output runs.
int menuChoice = 2;
switch (menuChoice) {
case 1:
cout << "Start game";
break;
case 2:
cout << "View profile";
break;
case 3:
cout << "Settings";
break;
default:
cout << "Invalid choice";
break;
}The position of break matters. It belongs after the statements that should run for that case and before the next case label. With menuChoice equal to 2, the path is case 2, its output, break, and then the code after the switch. Case 3 is present, but it is never reached for this choice.
Type the statement that must go immediately after the "View profile" output to stop execution from reaching case 3.
case 2:
cout << "View profile";
case 3:Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
For menuChoice equal to 2, default is skipped because case 2 matches. The switch does not run a matching case and then check default afterward. Default is the fallback path only when no listed case label matches the switch expression. Writing default last makes that fallback flow easiest to read, although its position does not change the matching rule.
switch (menuChoice) {
case 1:
cout << "Start game";
break;
case 2:
cout << "View profile";
break;
case 3:
cout << "Settings";
break;
default:
cout << "Invalid choice";
break;
}With the value 2, execution starts at case 2 and leaves through its break. It never reaches case 3 or default. If case 2 had no break, execution would still not move to default as a fallback, because it would simply continue downward from case 2 through the written case bodies.
This menu is a natural fit for switch because one expression, menuChoice, is compared with exact fixed values: 1, 2, and 3. Each value names one action. A case label is not a general condition, so it is not the place for a range such as menuChoice greater than or equal to 1 and less than or equal to 3, or for a compound condition using logical operators.
Use a switch when the decision is a list of exact alternatives for one expression. Range checks and unrelated conditions need condition-based logic instead, because they ask questions that fixed case labels cannot express. Keeping each exact menu value in its own case makes the selected action and its break visible together.