Loop Control in switch Context
Distinguish break in loops versus switches.
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")All lessons in this course
- break and continue Basics
- Labeled Statements
- Breaking Out of Nested Loops
- Loop Control in switch Context