Swift Academy · Lesson

Loop Control in switch Context

Distinguish break in loops versus switches.

Lesson 4 of 413 steps

Loop Control in switch Context is a free Swift Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

break in switch

Inside a switch, break ends the switch case. Swift cases do not fall through, so break is usually optional but allowed.

let n = 2
switch n {
case 1:
    print("one")
case 2:
    print("two")
    break
default:
    print("other")
}

Empty Case With break

Use break to make a case do nothing explicitly, which is clearer than an empty body.

let code = 0
switch code {
case 0:
    break // intentionally ignore
default:
    print("handled", code)
}
print("after switch")

break in a switch Inside a Loop

When a switch sits inside a loop, a plain break exits the switch, not the loop.

for n in 1...4 {
    switch n {
    case 3:
        break // exits switch only
    default:
        print(n)
    }
}
print("loop finished")

Exiting the Loop From a switch

To break out of the enclosing loop from inside a switch, label the loop and use break label.

loop: for n in 1...5 {
    switch n {
    case 3:
        break loop
    default:
        print(n)
    }
}
print("done")

continue From a switch

A plain continue inside a switch refers to the enclosing loop, skipping to its next iteration.

for n in 1...5 {
    switch n {
    case let x where x % 2 == 0:
        continue
    default:
        print(n)
    }
}

Why the Distinction Matters

Mixing up switch-break and loop-break is a common bug. Remember: bare break in a switch only leaves the switch.

for n in 1...3 {
    switch n {
    case 2:
        break
    default:
        print("value", n)
    }
}
print("all values processed")

fallthrough

If you actually want fall-through behavior, use the explicit fallthrough keyword.

let n = 1
switch n {
case 1:
    print("one")
    fallthrough
case 2:
    print("two")
default:
    print("done")
}

Labeled break for Clarity

Even when not strictly required, a labeled break documents that you mean the loop.

scan: for value in [10, 20, 30, 40] {
    switch value {
    case 30:
        break scan
    default:
        print(value)
    }
}

Combining where Clauses

Pattern where clauses plus continue let you filter elegantly inside a switch.

for n in 1...6 {
    switch n {
    case let x where x > 4:
        continue
    default:
        print(n)
    }
}

Nested Switch and Loop

With nested loops and a switch, labels make the target unambiguous.

outer: for i in 1...3 {
    for j in 1...3 {
        switch j {
        case 2:
            break outer
        default:
            print(i, j)
        }
    }
}

Best Practice

Use bare break only to leave a switch case; always label the loop when you intend to exit the loop from within a switch.

main: for n in 1...5 {
    switch n {
    case 4:
        break main
    default:
        print(n)
    }
}

Quick Check

Test switch-vs-loop break.

Recap

In a switch, a bare break exits only the switch case (Swift has no implicit fall-through; use fallthrough for that). To exit an enclosing loop from inside a switch, label the loop and use break label. A bare continue still refers to the loop.

loop: for n in 1...4 {
    switch n {
    case 3: break loop
    default: print(n)
    }
}
Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “Loop Control in switch Context” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Loop Control in switch Context”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “Loop Control in switch Context”?

Distinguish break in loops versus switches. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “Loop Control in switch Context” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. break and continue Basics
  2. Labeled Statements
  3. Breaking Out of Nested Loops
  4. Loop Control in switch Context
← Back to Swift Academy