Bird
0
0

Which of the following is the correct syntax for a switch case with a where clause in Swift?

easy📝 Syntax Q12 of 15
Swift - Control Flow

Which of the following is the correct syntax for a switch case with a where clause in Swift?

let number = 10
switch number {
    case let x where x > 5:
        print("Greater than 5")
    default:
        print("5 or less")
}
Acase let x if x > 5:
Bcase x where x > 5:
Ccase let x where x > 5:
Dcase x if x > 5:
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct pattern binding syntax

    In Swift, to bind a value in a case, use let x before the where clause.
  2. Step 2: Check the where clause usage

    The where clause follows the pattern binding, so case let x where x > 5: is correct.
  3. Final Answer:

    case let x where x > 5: -> Option C
  4. Quick Check:

    Pattern binding with let + where clause = case let x where x > 5: [OK]
Quick Trick: Use 'let' before variable, then 'where' for condition [OK]
Common Mistakes:
  • Omitting 'let' before variable
  • Using 'if' instead of 'where'
  • Placing 'where' before 'let'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes