ProgramGuru

Match cases with switch

Match cases with switch

A library desk needs different actions for open, closed and maintenance states. A switch statement compares one value with several cases. It is useful when alternatives are organized around that same value rather than unrelated conditions.

Swift switch statements must cover every possible value, often with a default case for types with many values. Cases do not automatically fall through into the next case. A case can match a value, range or pattern; where can add another condition.

What you’ll learn

  • Match closed and half-open ranges in an exhaustive switch.
  • Explain why ordinary cases do not fall through.

Match cases with switch in the example

  1. One value

    A library desk needs different actions for open, closed and maintenance states.

    One value
  2. Match a range

    Swift switch statements must cover every possible value, often with a default case for types with many values.

    Match a range
  3. One case executes

    The first range does not match 82.

    One case executes
The first range does not match 82. The half-open second range includes 75 but excludes 90, so Pass prints. The default is not reached. There is no need for break just to prevent automatic fallthrough.

Work through match cases with switch

Save this complete program as main.swift and run it with the Swift toolchain. No separate input file is needed.

Swift · complete program

let score = 82
switch score {
case 90...100: print("Excellent")
case 75..<90: print("Pass")
default: print("Review score")
}

Expected output

Pass

Follow the execution

The first range does not match 82. The half-open second range includes 75 but excludes 90, so Pass prints. The default is not reached. There is no need for break just to prevent automatic fallthrough.

Try this program in the Swift compiler. Paste the complete source above.

Practice: Match cases with switch

Your task: Which case handles score 90?

Swift · complete program

let score = 82
switch score {
case 90...100: print("Excellent")
case 75..<90: print("Pass")
default: print("Review score")
}

Worked answer

The first case handles it because the closed range 90...100 includes 90, while 75..<90 excludes that upper endpoint.

Validate before matching a default

The default here also catches impossible scores such as -5 or 120; a real form should validate the range explicitly. Intentional fallthrough has different semantics and should not be added as a substitute for understanding case matching.

Check your decision

Which case handles score 90?
Choose one answer. You can try again.

Answer and explanation

The first case handles it because the closed range 90...100 includes 90, while 75..<90 excludes that upper endpoint..

The first case handles it because the closed range 90...100 includes 90, while 75..<90 excludes that upper endpoint.

Match cases with switch in practice

The first case handles it because the closed range 90...100 includes 90, while 75..<90 excludes that upper endpoint. The default here also catches impossible scores such as -5 or 120; a real form should validate the range explicitly.

Further reading: official Swift language guide.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Repeat work with for-in →