SWIFT

Swift Switch Statement: Syntax, Usage, and Examples

A Swift switch statement lets you compare a value against multiple possible cases and run the matching block of code. It’s more flexible than if-else chains and supports complex patterns, making your logic easier to read and extend.

How to Use the Switch Statement in Swift

The switch statement Swift syntax starts with the switch keyword followed by the value you want to evaluate. Each case defines a match condition, and default acts as a fallback.

Basic Syntax of a Swift Switch Statement

let number = 3

switch number {
case 1:
    print("One")
case 2:
    print("Two")
case 3:
    print("Three")
default:
    print("Something else")
}

Swift automatically breaks out after each case—so you don’t need a break like in some other languages.

Matching Multiple Values

let character = "a"

switch character {
case "a", "e", "i", "o", "u":
    print("Vowel")
default:
    print("Consonant")
}

You can group multiple values in a single case using commas.

Using Ranges in Switch Cases

let score = 85

switch score {
case 90...100:
    print("Excellent")
case 70..<90:
    print("Good")
default:
    print("Needs improvement")
}

The switch statement in Swift works seamlessly with ranges and intervals.

When to Use the Swift Switch Statement

Reach for the Swift switch statement when:

  • You need to compare a value against many possible results.
  • You want cleaner logic than a bunch of if-else blocks.
  • Your matching patterns include ranges, tuples, or complex conditions.
  • You're writing UI logic (like checking device types or states).
  • You want more safety—Swift enforces that all values are handled unless you include a default.

Swift’s switch statement is a smart, readable way to branch logic.

Examples of Switch Statement Swift in Practice

Handling Strings

let command = "start"

switch command {
case "start":
    print("Starting the process...")
case "stop":
    print("Stopping now.")
case "pause":
    print("Pausing...")
default:
    print("Unknown command")
}

You can easily create readable commands or instructions using string-based matching.

Reacting to Integer Ranges

let temperature = 75

switch temperature {
case ..<0:
    print("Freezing")
case 0..<60:
    print("Cold")
case 60..<80:
    print("Comfortable")
default:
    print("Hot")
}

You can build simple decision trees with minimal code.

Pattern Matching Tuples

let point = (0, 0)

switch point {
case (0, 0):
    print("Origin")
case (_, 0):
    print("X-axis")
case (0, _):
    print("Y-axis")
default:
    print("Somewhere else")
}

Swift switch statements support pattern matching beyond simple values.

Checking Enums

enum Status {
    case loading, success, error
}

let current = Status.success

switch current {
case .loading:
    print("Loading data...")
case .success:
    print("Data loaded successfully")
case .error:
    print("Something went wrong")
}

This pattern is common in SwiftUI or state-based architecture.

Learn More About Switch Statements in Swift

No Fallthrough by Default

In other languages, cases “fall through” unless you add break. Swift avoids that by default. If you do want fallthrough, you must write it explicitly:

let value = 5

switch value {
case 5:
    print("Five")
    fallthrough
default:
    print("Default ran too")
}

Use fallthrough carefully—it’s rare in idiomatic Swift.

Value Binding

You can bind values to temporary variables inside a case:

let point = (3, 0)

switch point {
case (let x, 0):
    print("On the x-axis at \(x)")
case (0, let y):
    print("On the y-axis at \(y)")
default:
    print("Somewhere else")
}

This makes the switch statement Swift-friendly and flexible.

Where Clauses

Use where for additional conditions in a case:

let number = 10

switch number {
case let x where x % 2 == 0:
    print("Even")
default:
    print("Odd")
}

This lets you filter or refine matches dynamically.

Switch and Optionals

Use switch to safely unwrap and process optionals:

let name: String? = "Taylor"

switch name {
case .some(let value):
    print("Hello, \(value)")
case .none:
    print("No name found")
}

Although if let is often used, switch gives you more control.

Enums with Associated Values

Swift’s switch really shines with enums that hold values:

enum Media {
    case image(name: String)
    case video(length: Int)
}

let file = Media.image(name: "photo.jpg")

switch file {
case .image(let name):
    print("Image: \(name)")
case .video(let length):
    print("Video length: \(length) seconds")
}

This kind of logic is very readable and powerful in Swift apps.

Switch as Expression

Although Swift doesn’t let you use switch as an expression like some other languages, you can return values from inside switch cases:

func classify(_ number: Int) -> String {
    switch number {
    case ..<0:
        return "Negative"
    case 0:
        return "Zero"
    default:
        return "Positive"
    }
}

This is clean and easy to understand at a glance.

Best Practices for Swift Switch Statements

  • Always cover all possible cases when switching over enums.
  • Prefer specific matches before generic ones.
  • Use where for cleaner case filters instead of nesting if conditions.
  • Group logic meaningfully—don’t cram too many actions into one case.
  • Don’t forget the default case unless your enum or value is exhaustive.
  • Avoid using switch just to replace a single if/else—use it for clarity.

The Swift switch statement is more than just a control flow tool—it’s a powerful way to express logic clearly, safely, and concisely. With support for pattern matching, value binding, ranges, and enums, Swift’s version of switch goes far beyond basic conditional branching.

Learn to Code in Swift for Free
Start learning now
button icon
To advance beyond this tutorial and learn Swift by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

You can code, too.

© 2025 Mimo GmbH