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?✗ Incorrect
The
break keyword ends the current case and prevents running the next cases.Which part of a
switch runs if no cases match?✗ Incorrect
The
default case runs when no other case matches the variable.Can you use a
switch statement with a float variable in C++?✗ Incorrect
C++
switch only works with integral types like int or char, not float.What happens if you omit
break in a case?✗ Incorrect
Without
break, execution continues into the next case(s), called fall-through.Which of these is a valid
switch variable type?✗ 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.