0
0
Swiftprogramming~5 mins

Switch with compound cases in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a compound case in a Swift switch statement?
A compound case lets you group multiple values together in one case using commas, so the same code runs for any of those values.
Click to reveal answer
beginner
How do you write multiple values in one case in Swift's switch?
You list the values separated by commas after the case keyword, like case 1, 2, 3:.
Click to reveal answer
beginner
Why use compound cases in a switch statement?
It makes your code shorter and clearer by handling several values with the same code instead of repeating cases.
Click to reveal answer
intermediate
Can you use ranges or conditions with compound cases in Swift?
Yes, you can combine compound cases with ranges or use where clauses for extra conditions.
Click to reveal answer
beginner
Example: What does this Swift code print?<br><pre>let number = 3
switch number {
 case 1, 3, 5:
  print("Odd number")
 case 2, 4, 6:
  print("Even number")
 default:
  print("Unknown")
}</pre>
It prints Odd number because 3 matches the first compound case with 1, 3, and 5.
Click to reveal answer
In Swift, how do you group multiple values in one switch case?
ASeparate values with commas after case
BUse semicolons between values
CWrite multiple case lines for each value
DUse parentheses around values
What happens if a value matches a compound case in Swift switch?
AIt runs all cases
BThe switch skips that case
CThe code inside that case runs
DThe program crashes
Can you mix ranges and compound cases in Swift switch?
AYes, you can combine them
BNo, ranges and compound cases are exclusive
COnly if you use if-else inside switch
DOnly with default case
Which keyword lets you add extra conditions to a Swift switch case?
Aelse
Bif
Ccase
Dwhere
What is the benefit of using compound cases in Swift switch?
ASlower program execution
BShorter and clearer code
CMore lines of code
DAvoids using default case
Explain how to use compound cases in a Swift switch statement and why they are useful.
Think about how you can handle several options together without repeating code.
You got /3 concepts.
    Write a Swift switch statement using compound cases to print "Weekend" for Saturday and Sunday, and "Weekday" for other days.
    Use compound cases to group weekend days together.
    You got /3 concepts.