Bird
0
0

Identify the error in this Swift switch statement using value binding:

medium📝 Debug Q14 of 15
Swift - Control Flow
Identify the error in this Swift switch statement using value binding:
let number = 5
switch number {
 case let x where x > 0:
     print("Positive")
 case let x where x < 0:
     print("Negative")
 case x == 0:
     print("Zero")
}
AMissing 'let' keyword in the last case
BIncorrect use of 'where' in first two cases
CInvalid pattern syntax in the last case
DSwitch statement missing default case
Step-by-Step Solution
Solution:
  1. Step 1: Review last case syntax

    The last case uses case x == 0:, which is not valid pattern syntax in Swift.
  2. Step 2: Correct pattern usage

    It should use value binding with a where clause, like case let x where x == 0:.
  3. Final Answer:

    Invalid pattern syntax in the last case -> Option C
  4. Quick Check:

    Pattern must be 'case let x where condition' [OK]
Quick Trick: Use 'case let x where condition' for conditions [OK]
Common Mistakes:
  • Writing conditions directly after case without let
  • Omitting where keyword
  • Assuming default case is mandatory

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes