Compound Cases for Switch Statements in Swift

Learn how to group multiple switch cases together so related values can run the same code without repeating yourself.
Written by

Iñaki Narciso

Updated on

Apr 06 2026

Table of contents

    Overview

    A compound case lets one case in a Swift switch statement match more than one value. Instead of writing the same code block under several separate cases, you list the matching values together with commas.

    This is especially useful with enums. You still get the compiler safety of switching over a fixed set of values, but your code stays shorter when several values should behave the same way.

    The Problem Compound Cases Solve

    Imagine you want to group planets by where they sit relative to the asteroid belt.

    enum Planet {
        case mercury
        case venus
        case earth
        case mars
        case jupiter
        case saturn
        case uranus
        case neptune
    }
    
    let planet: Planet = .earth

    You could write a separate case for every planet, but several planets need the same message. That is where beginners sometimes reach for fallthrough.

    The Fallthrough Version

    Here is one way to make multiple cases run the same code:

    switch planet {
    case .mercury:
        fallthrough
    case .venus:
        fallthrough
    case .earth:
        fallthrough
    case .mars:
        print("This planet is before the asteroid belt")
    
    case .jupiter:
        print("This planet is near the asteroid belt")
    
    case .saturn:
        fallthrough
    case .uranus:
        fallthrough
    case .neptune:
        print("This planet is beyond the asteroid belt")
    }

    The fallthrough keyword tells Swift to continue into the next case instead of stopping after the current match.

    For example, if planet is .earth, Swift matches case .earth, sees fallthrough, moves into case .mars, runs the print statement, and then exits the switch.

    Important: fallthrough exists in Swift, but it is not the usual way to group simple matching cases. Compound cases are clearer for this job.

    The Compound Case Version

    A compound case lists multiple matching values on the same case line:

    switch planet {
    case .mercury, .venus, .earth, .mars:
        print("This planet is before the asteroid belt")
    
    case .jupiter:
        print("This planet is near the asteroid belt")
    
    case .saturn, .uranus, .neptune:
        print("This planet is beyond the asteroid belt")
    }

    This version does the same thing as the fallthrough example, but it is shorter and easier to read. Each line says exactly which values share the same behavior.

    Compound Cases with Strings

    Compound cases also work with strings, numbers, and other values. Here is a simple command example:

    let command = "start"
    
    switch command {
    case "start", "run", "begin":
        print("Starting...")
    case "stop", "cancel", "end":
        print("Stopping...")
    default:
        print("Unknown command")
    }

    Use this pattern when several inputs should trigger the same result.

    Compound Cases with Associated Values

    Compound cases can also work with enum cases that carry associated values, as long as the patterns bind the same values in a compatible way.

    enum AppRoute {
        case profile(userID: Int)
        case settings(userID: Int)
        case help
    }
    
    let route = AppRoute.settings(userID: 42)
    
    switch route {
    case .profile(let userID), .settings(let userID):
        print("User-specific screen: \\(userID)")
    case .help:
        print("Help screen")
    }

    Both .profile and .settings bind a local userID, so they can share the same code block.

    When to Use Compound Cases

    • Use compound cases when multiple values should run exactly the same code.
    • Use separate cases when the values need different behavior, even if the code looks similar at first.
    • Avoid fallthrough for simple grouping. It makes the control flow harder to scan.
    • Keep compound cases short enough that the reader can understand the group at a glance.

    Common Mistakes

    Using fallthrough when commas would be clearer

    If your only goal is to make several values run one shared block of code, a compound case is usually the better choice.

    Grouping values that are not really the same

    Do not group cases just because their code is currently similar. Group them when they mean the same thing in your app's logic.

    Forgetting the default case for open-ended values

    When switching over a string or integer, Swift usually cannot know every possible input. Add a default case unless your switch is otherwise exhaustive.

    Quick Reference

    PatternWhat It Does
    case .a, .b:Matches either enum case and runs the same block
    case "start", "run":Matches either string value
    case 1, 2, 3:Matches any listed numeric value
    case .profile(let id), .settings(let id):Matches either associated-value case and binds the same local value
    fallthroughContinues into the next case; useful rarely, but not needed for simple grouping

    Summary

    • Compound cases let one switch case match multiple values.
    • They are written by separating values with commas on the same case line.
    • They are usually cleaner than fallthrough for grouping values.
    • They work with enums, strings, numbers, and associated-value patterns.
    • Use them when the grouped values genuinely share the same behavior.

    The main idea is simple: when several values mean the same thing to your program, let one case handle them together. It keeps your switch statements compact without giving up Swift's readability.