0Pricing
Swift Academy · Lesson

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

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