0
0
C++programming~5 mins

Switch statement in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a switch statement used for in C++?
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
Which part of a switch runs if no cases match?
Acase 0
Belse
Cfinal
Ddefault
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
What happens if you omit break in a case?
AProgram crashes
BRuns next case(s) too
CSkips to default
DCompiler error
Which of these is a valid switch variable type?
Astring
Bdouble
Cchar
Dfloat
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.