Thuta Learning
IntermediateProgrammingbeginner

Switch Statements

Relax. We'll talk through this in plain words — no textbook voice.

switch is useful for checking a single value precisely against many cases. It helps you cleanly organize things like menu selection, app routes, user status, payment status, and error types. Swift's switch also supports ranges, multiple cases, pattern matching, where conditions too.

swift
let paymentStatus = "pending"

switch paymentStatus {
case "paid":
    print("Access granted")
case "pending":
    print("Payment is still processing")
case "failed", "cancelled":
    print("Please try payment again")
default:
    print("Unknown payment status")
}

paymentStatus value is checked against each case. pending matches, so it outputs the processing message. In a Swift switch, if you don't cover every case, you need to add a default case.

You should see
Payment is still processing

Easy traps

  • Dumping everything into default without thinking through the possible cases can make debugging harder.
Switch Statements | Thuta Learning