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