A switch statement lets you choose between many options based on the value of a single variable. It helps run different code blocks depending on that value.
Click to reveal answer
beginner
How do you write a basic switch statement syntax in C++?
You write switch(variable) { case value1: //code break; case value2: //code break; default: //code }. Each case checks if the variable matches a value, and break stops the code from running into the next case.
Click to reveal answer
intermediate
What happens if you forget to put break in a case inside a switch?
If you forget break, the program will continue running the next cases too, even if their values don't match. This is called "fall-through." Sometimes it's useful, but often it causes bugs.
Click to reveal answer
beginner
What is the purpose of the default case in a switch statement?
The default case runs if none of the other case values match the variable. It's like an "else" to catch all other possibilities.
Click to reveal answer
intermediate
Can switch statements in C++ work with strings?
No, C++ switch statements only work with integral types like int, char, or enums. For strings, you need to use if-else chains.
Click to reveal answer
What keyword stops execution from falling through to the next case in a switch?
Abreak
Bcontinue
Cstop
Dexit
✗ Incorrect
The break keyword ends the current case and prevents running the next cases.
Which part of a switch runs if no cases match?
Acase 0
Belse
Cfinal
Ddefault
✗ Incorrect
The default case runs when no other case matches the variable.
Can you use a switch statement with a float variable in C++?
ANo, only integral types
BYes, always
COnly with <code>break</code>
DOnly if you cast it to int
✗ Incorrect
C++ switch only works with integral types like int or char, not float.
What happens if you omit break in a case?
AProgram crashes
BRuns next case(s) too
CSkips to default
DCompiler error
✗ Incorrect
Without break, execution continues into the next case(s), called fall-through.
Which of these is a valid switch variable type?
Astring
Bdouble
Cchar
Dfloat
✗ Incorrect
Only integral types like char can be used in switch statements.
Explain how a switch statement works and why break is important.
Think of a menu where you pick one option and stop.
You got /4 concepts.
Describe when you would use a switch instead of if-else statements.
Imagine choosing from many fixed choices quickly.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a switch statement in C++?
easy
A. To declare a variable
B. To repeat a block of code multiple times
C. To define a function
D. To choose which block of code to run based on a single value
Solution
Step 1: Understand the role of switch
A switch statement checks one value and runs code matching that value.
Step 2: Compare with other options
Repeating code is done by loops, functions define reusable code, and variables store data.
Final Answer:
To choose which block of code to run based on a single value -> Option D
Quick Check:
Switch selects code by value = A [OK]
Hint: Switch picks code based on one value quickly [OK]
Common Mistakes:
Confusing switch with loops
Thinking switch defines functions
Mixing switch with variable declaration
2. Which of the following is the correct syntax to start a switch statement for variable choice?
easy
A. switch(choice) {
B. switch choice {
C. switch choice() {
D. switch(choice);
Solution
Step 1: Recall switch syntax
The switch statement requires parentheses around the variable: switch(variable).
Step 2: Check each option
switch(choice) { uses correct parentheses and opening brace. Others miss parentheses or use wrong punctuation.
Final Answer:
switch(choice) { -> Option A
Quick Check:
Correct switch syntax uses parentheses [OK]
Hint: Switch needs parentheses around variable [OK]
Common Mistakes:
Omitting parentheses
Using semicolon instead of brace
Adding parentheses after variable name
3. What will be the output of this code?
int x = 2;
switch(x) {
case 1:
std::cout << "One";
break;
case 2:
std::cout << "Two";
break;
default:
std::cout << "Other";
}
medium
A. One
B. Two
C. Other
D. No output
Solution
Step 1: Check the value of x
The variable x is 2.
Step 2: Match case in switch
The switch matches case 2, so it prints "Two" and then breaks.
Final Answer:
Two -> Option B
Quick Check:
Value 2 matches case 2 = Two [OK]
Hint: Match variable value to case label [OK]
Common Mistakes:
Forgetting break causes fall-through
Choosing default when case matches
Confusing output with case number
4. Identify the error in this switch statement:
int n = 3;
switch(n) {
case 1:
std::cout << "One";
case 2:
std::cout << "Two";
break;
default:
std::cout << "Default";
}
medium
A. default case must be last
B. Variable n should be a string
C. Missing break after case 1 causes fall-through
D. case labels must be strings
Solution
Step 1: Check case 1 block
There is no break; after case 1, so execution falls through to case 2.
Step 2: Understand consequences
Without break, both "One" and "Two" print if n=1. This is usually unintended.
Final Answer:
Missing break after case 1 causes fall-through -> Option C
Quick Check:
Break prevents fall-through = A [OK]
Hint: Always add break after each case to stop fall-through [OK]
Common Mistakes:
Forgetting break causes multiple outputs
Thinking default must be last (it can be anywhere)
Believing case labels must be strings
5. You want to write a switch statement that prints "Weekend" for cases 6 and 7, and "Weekday" for cases 1 to 5. Which code snippet correctly does this?
hard
A. switch(day) {
case 1: case 2: case 3: case 4: case 5:
std::cout << "Weekday";
break;
case 6: case 7:
std::cout << "Weekend";
break;
default:
std::cout << "Invalid";
}
B. switch(day) {
case 1:
case 2:
case 3:
case 4:
case 5:
std::cout << "Weekend";
break;
case 6:
case 7:
std::cout << "Weekday";
break;
default:
std::cout << "Invalid";
}
C. switch(day) {
case 1:
case 2:
case 3:
case 4:
case 5:
std::cout << "Weekday";
case 6:
case 7:
std::cout << "Weekend";
break;
default:
std::cout << "Invalid";
}
D. switch(day) {
case 1: case 2: case 3: case 4: case 5:
std::cout << "Weekend";
case 6: case 7:
std::cout << "Weekday";
break;
default:
std::cout << "Invalid";
}
Solution
Step 1: Group cases for weekdays and weekends
Cases 1 to 5 print "Weekday"; cases 6 and 7 print "Weekend". Grouping cases with no code between them is correct.
Step 2: Check breaks and outputs
switch(day) {
case 1: case 2: case 3: case 4: case 5:
std::cout << "Weekday";
break;
case 6: case 7:
std::cout << "Weekend";
break;
default:
std::cout << "Invalid";
} groups cases correctly and uses breaks to prevent fall-through. Others mix outputs or miss breaks.
Final Answer:
Option A code correctly prints "Weekday" for 1-5 and "Weekend" for 6-7 -> Option A
Quick Check:
Group cases + breaks = correct switch [OK]
Hint: Group cases without code, add break after each group [OK]
Common Mistakes:
Missing breaks causing fall-through
Swapping outputs for cases
Not grouping cases properly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.