Introduction
Control transfer statements change the order in which code executes by transferring control from one piece of code to another. Swift provides five of them: continue, break, fallthrough, return, and labeled statements. continue skips the rest of the current loop iteration and moves to the next one. break exits a loop or switch statement entirely. fallthrough explicitly forces a switch case to continue into the next case, since Swift's switch doesn't do this automatically. return exits a function, optionally passing back a value. Labeled statements let you tag a loop with a name so that break or continue can target a specific outer loop from within nested loops.
Cricket analogy: Looping over each ball of an over, continue is like the umpire calling a no-ball and moving straight to the next delivery; break is calling off play entirely for bad light; fallthrough is like a rain-delay ruling explicitly carrying over into the next session's conditions rather than resetting; return is the innings declaration handing control back to the opposition; and a labeled oversLoop: lets break oversLoop end the entire innings from within a single ball's review.
Syntax
outerLoop: for i in 1...3 {
for j in 1...3 {
if j == 2 {
continue outerLoop
}
if i == 3 {
break outerLoop
}
print("i=\(i), j=\(j)")
}
}Explanation
A label, like outerLoop:, is placed immediately before a loop's keyword and is followed by a colon. Inside a nested loop, continue outerLoop skips directly to the next iteration of the labeled outer loop rather than the inner one, and break outerLoop exits the outer loop entirely rather than just the inner loop. Without a label, break and continue only affect the innermost enclosing loop or switch. fallthrough is used inside a switch case when you deliberately want execution to continue into the next case's body, bypassing Swift's normal exhaustive-and-exit behavior. return works inside functions to exit immediately, optionally returning a value to the caller.
Cricket analogy: A label like inningsLoop: sits right before the loop keyword; inside a nested over-by-over loop, continue inningsLoop skips straight to the next innings rather than just the next ball, and break inningsLoop ends the whole match rather than just the current over; without a label, break only stops the innermost over; fallthrough in a switch on match result deliberately carries a 'win' case into the 'net run rate updated' case; and return inside a scoring function exits immediately with the final total.
Example
for number in 1...5 {
if number == 3 {
continue
}
if number == 5 {
break
}
print(number)
}
switch 2 {
case 1:
print("one")
case 2:
print("two")
fallthrough
case 3:
print("three")
default:
print("other")
}Output
1
2
4
two
threeKey Takeaways
- continue skips the rest of the current iteration and moves on to the next one.
- break exits a loop or switch statement immediately and completely.
- fallthrough explicitly continues execution into the next switch case.
- return exits a function immediately, optionally returning a value.
- Labeled statements (e.g. outerLoop:) let break and continue target a specific enclosing loop from nested loops.
Practice what you learned
1. What does continue do inside a loop?
2. How do you make a Swift switch case continue into the next case's code?
3. What is the purpose of a labeled statement like outerLoop: for ... { }?
4. Without a label, which loop does an unqualified break affect inside nested loops?
Was this page helpful?
You May Also Like
Loops in Swift (for-in, while, repeat-while)
Understand Swift's three loop types — for-in, while, and repeat-while — since the C-style for loop was removed.
switch Statement in Swift
Explore Swift's powerful switch statement, which requires exhaustiveness and never falls through by default.
if-else Statements in Swift
Learn how Swift's if-else statement branches execution using strictly typed Bool conditions and mandatory braces.
Functions in Swift
Learn how to declare, call, and label the parameters of reusable blocks of code called functions in Swift.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics