Recall & Review
beginner
What is the purpose of a switch statement in C#?
A switch statement allows you to choose between many options based on the value of a variable. It helps to run different code blocks depending on the value, making the code easier to read than many if-else statements.
Click to reveal answer
beginner
How does the switch statement decide which case to execute?
The switch statement compares the value of the expression with each case label. When it finds a matching case, it executes the code inside that case until it hits a break or the end of the switch.
Click to reveal answer
beginner
What happens if no case matches in a switch statement?
If no case matches, the code inside the default case runs if it exists. The default case acts like a fallback for any value not covered by other cases.
Click to reveal answer
beginner
Why is the break statement important in a switch case?
The break statement stops the switch from running into the next case. Without break, the program continues executing the next cases, which can cause unexpected behavior.
Click to reveal answer
intermediate
Can a switch statement in C# handle multiple cases with the same code?
Yes, you can list multiple case labels one after another without break statements between them. This way, all those cases run the same code block.
Click to reveal answer
What keyword ends a case block in a C# switch statement to prevent fall-through?
✗ Incorrect
The break keyword stops execution inside the switch and prevents running into the next case.
What happens if you omit the break statement in a switch case?
✗ Incorrect
Without break, execution continues into the next case, which is called fall-through.
Which case runs if no other case matches and a default case is present?
✗ Incorrect
The default case acts as a fallback when no other case matches.
Can a switch statement in C# use strings as case labels?
✗ Incorrect
C# supports string values in switch case labels.
How do you group multiple cases to run the same code in a switch?
✗ Incorrect
Listing cases consecutively without break groups them to run the same code.
Explain how a switch statement executes when given a value. Include what happens when a matching case is found and the role of break.
Think about how the program picks and runs one case among many.
You got /3 concepts.
Describe how you can handle multiple values with the same code block inside a switch statement.
Consider how to avoid repeating code for similar cases.
You got /3 concepts.