Loop Control in switch Context
Distinguish break in loops versus switches.
Loop Control in switch Context is a free Swift Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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)
}
}Frequently asked questions
Is the “Loop Control in switch Context” lesson free?
Yes — the full text of “Loop Control in switch Context” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
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
- break and continue Basics
- Labeled Statements
- Breaking Out of Nested Loops
- Loop Control in switch Context