Bird
Raised Fist0
Swiftprogramming~20 mins

Switch with where clauses in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swift Switch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of switch with where clause filtering
What is the output of this Swift code using a switch with a where clause?
Swift
let number = 15
switch number {
case let x where x % 3 == 0 && x % 5 == 0:
    print("FizzBuzz")
case let x where x % 3 == 0:
    print("Fizz")
case let x where x % 5 == 0:
    print("Buzz")
default:
    print(number)
}
AFizz
BFizzBuzz
CBuzz
D15
Attempts:
2 left
💡 Hint
Remember the order of cases and how where clauses filter values.
❓ Predict Output
intermediate
2:00remaining
Switch with multiple where clauses
What will this Swift code print?
Swift
let point = (x: 3, y: 4)
switch point {
case let (x, y) where x == y:
    print("On the diagonal")
case let (x, y) where x > y:
    print("Above the diagonal")
case let (x, y) where x < y:
    print("Below the diagonal")
default:
    print("Unknown")
}
AOn the diagonal
BAbove the diagonal
CUnknown
DBelow the diagonal
Attempts:
2 left
💡 Hint
Compare x and y values carefully.
🔧 Debug
advanced
2:00remaining
Identify the error in switch with where clause
This Swift code does not compile. What is the error?
Swift
let value = 10
switch value {
case let x where x > 5:
    print("Greater than 5")
case let x where x <= 5:
    print("5 or less")
}
AMissing default case
BCannot use let binding in switch cases
CMissing colon ':' after the first case's where clause
DCannot use comparison operators in where clause
Attempts:
2 left
💡 Hint
Check punctuation after case statements.
🚀 Application
advanced
2:00remaining
Using switch with where to classify ages
Given this Swift code, what is the output?
Swift
let age = 20
switch age {
case let x where x >= 0 && x <= 12:
    print("Child")
case let x where x >= 13 && x <= 19:
    print("Teenager")
case let x where x >= 20 && x <= 64:
    print("Adult")
case let x where x >= 65:
    print("Senior")
default:
    print("Invalid age")
}
AAdult
BTeenager
CSenior
DInvalid age
Attempts:
2 left
💡 Hint
Check which age range 20 falls into.
🧠 Conceptual
expert
3:00remaining
Behavior of switch with overlapping where clauses
Consider this Swift code snippet. Which case will execute and why?
Swift
let number = 8
switch number {
case let x where x > 5:
    print("Greater than 5")
case let x where x > 3:
    print("Greater than 3")
default:
    print("3 or less")
}
APrints "Greater than 5" because the first matching case is executed
BPrints "Greater than 3" because it is the more specific condition
CPrints "3 or less" because default always runs
DCompilation error due to overlapping where clauses
Attempts:
2 left
💡 Hint
Switch executes the first matching case only.

Practice

(1/5)
1.

What does a where clause do in a Swift switch statement?

easy
A. It defines the default case in the switch statement.
B. It adds an extra condition to a case to check before matching.
C. It repeats the case multiple times automatically.
D. It changes the type of the value being switched.

Solution

  1. Step 1: Understand the role of where clause

    The where clause adds an extra condition to a case in a switch statement.
  2. Step 2: Compare with other options

    Other options describe unrelated behaviors like default case or type changes, which are not what where does.
  3. Final Answer:

    It adds an extra condition to a case to check before matching. -> Option B
  4. Quick Check:

    where clause = extra condition [OK]
Hint: Remember: where adds conditions to cases [OK]
Common Mistakes:
  • Thinking where defines default case
  • Confusing where with case repetition
  • Assuming where changes variable types
2.

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")
}
easy
A. case let x if x > 5:
B. case x where x > 5:
C. case let x where x > 5:
D. case x if x > 5:

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]
Hint: Use 'let' before variable, then 'where' for condition [OK]
Common Mistakes:
  • Omitting 'let' before variable
  • Using 'if' instead of 'where'
  • Placing 'where' before 'let'
3.

What will be printed by this Swift code?

let age = 20
switch age {
case let x where x < 13:
    print("Child")
case let x where x >= 13 && x < 20:
    print("Teenager")
case let x where x >= 20:
    print("Adult")
default:
    print("Unknown")
}
medium
A. Adult
B. Teenager
C. Child
D. Unknown

Solution

  1. Step 1: Check the value of age

    The variable age is 20.
  2. Step 2: Evaluate each case condition

    First case: x < 13? No (20 < 13 is false). Second case: 13 <= x < 20? No (20 < 20 is false). Third case: x >= 20? Yes (20 >= 20 is true).
  3. Final Answer:

    Adult -> Option A
  4. Quick Check:

    age 20 matches 'Adult' case [OK]
Hint: Check conditions in order; first true case runs [OK]
Common Mistakes:
  • Choosing 'Teenager' because 20 is close to 19
  • Ignoring >= and < operators
  • Forgetting switch stops at first match
4.

Identify the error in this Swift switch statement using where clauses:

let score = 85
switch score {
case let x where x > 90:
    print("Excellent")
case let x where x >= 80:
    print("Good")
case let x where x >= 70:
    print("Average")
case _:
    print("Poor")
}
medium
A. Using let binding is not allowed with where.
B. Missing default case for unmatched values.
C. Incorrect syntax for where clause.
D. The cases overlap and order causes wrong matching.

Solution

  1. Step 1: Analyze case conditions and order

    The cases check if score > 90, then >= 80, then >= 70. Since 85 >= 80 and also >= 70, the second case matches first.
  2. Step 2: Understand overlapping conditions effect

    Because cases overlap, order matters. If order was different, output might change. This can cause confusion or wrong matching.
  3. Final Answer:

    The cases overlap and order causes wrong matching. -> Option D
  4. Quick Check:

    Overlapping where clauses need careful order [OK]
Hint: Order cases from most specific to least specific [OK]
Common Mistakes:
  • Ignoring overlapping conditions
  • Assuming default case is mandatory
  • Thinking let binding disallowed with where
5.

You want to categorize a number as "Small", "Medium", or "Large" using a switch with where clauses. The rules are:

  • Small: less than 10
  • Medium: between 10 and 100 inclusive
  • Large: greater than 100

Which switch statement correctly implements this?

let value = 50
switch value {
    case let x where x < 10:
        print("Small")
    case let x where x >= 10 && x <= 100:
        print("Medium")
    case let x where x > 100:
        print("Large")
    default:
        print("Unknown")
}
hard
A. Correctly categorizes all values as per rules.
B. Fails to categorize values equal to 10 or 100.
C. Has syntax errors in where clauses.
D. Default case will never be reached.

Solution

  1. Step 1: Check each case condition against rules

    First case: x < 10 matches "Small". Second case: x >= 10 and x <= 100 matches "Medium". Third case: x > 100 matches "Large".
  2. Step 2: Verify coverage and syntax

    All values fit one of these cases. Syntax is correct with let x where clauses. Default is fallback but not needed here.
  3. Final Answer:

    Correctly categorizes all values as per rules. -> Option A
  4. Quick Check:

    Conditions cover all ranges correctly [OK]
Hint: Use clear, non-overlapping where conditions [OK]
Common Mistakes:
  • Overlapping ranges causing wrong matches
  • Missing <= or >= in conditions
  • Forgetting to bind variable with let
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard