Swift break statement
The Swift break statement ends the execution of a loop or switch statement immediately. Program control then moves to the first statement after that loop or switch.
Use break when a loop has already found the required value, reached a stopping condition, or should not process any more elements. Swift supports break in while loops, repeat-while loops, for-in loops, and switch statements.
How Swift break changes control flow
Without break, a loop normally continues until its condition becomes false or its sequence is exhausted. When Swift encounters break, it skips the remaining statements in the current iteration, stops the loop completely, and continues with the code after the loop.
breakexits the entire nearest loop orswitch.- No later statements in the current loop iteration are executed.
- No additional loop iterations begin.
- A labeled
breakcan exit a specific enclosing statement.
Swift break statement syntax
Following is syntax of Swift break statement in a program.
break
In while loop
while boolean_expression {
// some statements
break
// some other statements
}
In for loop
for index in var {
// some statements
break
// some other statements
}
In repeat while loop
repeat {
// some statements
break
// some other statements
} while boolean_expression
An unlabeled break exits the innermost enclosing loop or switch. To exit an outer loop from nested control flow, assign a label to that outer loop and use break labelName.
Swift break example with a while loop
In this example, we will use break statement in swift while loop. For some reason, consider that we would like to break the loop when i becomes 3.
main.swift
var i = 0
while i < 5 {
if i==3 {
break
}
print("i : \(i)")
i = i + 1
}
print("rest of the statements")
Output
$swift while_loop_break_example.swift
i : 0
i : 1
i : 2
rest of the statements
When i reaches 3, the condition inside the loop is true and break exits the loop before the value is printed. Execution then continues with print("rest of the statements").
Swift break example with a for-in loop
In this example, we will use break statement in swift for loop. For some reason, consider that we would like to break the loop when i becomes 3.
main.swift
var i = 0
for i in 1..<6 {
if i==3 {
break
}
print("i : \(i)")
}
print("rest of the statements")
Output
$swift for_loop_break_example.swift
i : 1
i : 2
rest of the statements
The range contains the values from 1 through 5, but the loop ends when i == 3. Therefore, only 1 and 2 are printed.
Swift break example with a repeat-while loop
In this example, we will use break statement in swift repeat while loop. For some reason, consider that we would like to break the loop when i becomes 3.
main.swift
var i = 0
repeat {
if i==3 {
break
}
print("i : \(i)")
i = i + 1
} while i<5
print("rest of the statements")
Output
$swift repeat_while_loop_break_example.swift
i : 0
i : 1
i : 2
rest of the statements
A repeat-while loop checks its condition after running the body. In this example, however, break stops the loop as soon as i becomes 3, before the trailing condition is checked again.
Using break in a Swift switch statement
Swift switch cases do not fall through to the next case automatically, so a break statement is not required at the end of every case. It is useful when a case must intentionally perform no action because Swift does not allow an empty case body.
let command = "skip"
switch command {
case "start":
print("Starting")
case "skip":
break
case "stop":
print("Stopping")
default:
print("Unknown command")
}
print("Switch finished")
Output
Switch finished
The value matches the "skip" case. The break statement ends the switch without printing anything from that case, and execution continues after the closing brace.
Exiting nested loops with a labeled break in Swift
Inside nested loops, an ordinary break exits only the innermost loop. A statement label lets you name the outer loop and exit it directly.
outerLoop: for row in 1...3 {
for column in 1...3 {
if row == 2 && column == 2 {
break outerLoop
}
print("row: \(row), column: \(column)")
}
}
print("Nested loops finished")
Output
row: 1, column: 1
row: 1, column: 2
row: 1, column: 3
row: 2, column: 1
Nested loops finished
When both values are 2, break outerLoop exits the labeled outer loop. Without the label, only the inner loop would end.
Difference between break and continue in Swift
| Statement | Effect on the current iteration | Effect on the loop |
|---|---|---|
break | Stops the current iteration immediately | Ends the loop completely |
continue | Skips the remaining statements in the current iteration | Starts the next iteration when one is available |
Use break when no more loop iterations are needed. Use continue when only the current item should be skipped and the loop should keep running.
Common Swift break mistakes
- Expecting code after break to run: statements that follow
breakin the same loop iteration are skipped. - Breaking the wrong nested statement: an unlabeled
breakexits only the nearest loop orswitch. Use a label when the outer statement must end. - Adding break after every switch case: Swift ends a matched case automatically unless
fallthroughis used. - Using break where continue is intended:
breakstops all remaining iterations, whilecontinueskips only one iteration. - Forgetting progress in a while loop: when a loop does not reach its
breakcondition and its control variable is not updated, it may run indefinitely.
Swift break usage checklist
- Confirm that the stopping condition is checked before statements that must not run.
- Verify whether the nearest loop or an outer labeled loop should be terminated.
- Check that a
switchcase usesbreakonly when an executable no-op case is needed. - Test the boundary value that triggers
breakand the statement immediately after the loop. - Confirm that
continueis not more appropriate when later iterations should still run.
Swift break statement FAQs
What is break in Swift?
break is a control transfer statement that immediately ends the nearest enclosing loop or switch. Execution resumes with the first statement after that control-flow statement.
What is the difference between break and continue in Swift?
break ends the entire loop. continue ends only the current iteration and allows the loop to proceed with the next iteration.
Does Swift require break after each switch case?
No. Swift does not implicitly fall through from one case to the next, so a matched case finishes without a trailing break. Use break when a case intentionally has no other executable work.
How do you break out of nested loops in Swift?
Add a label before the outer loop, such as outerLoop:, and use break outerLoop from inside the nested loop.
Swift break key takeaways
In this Swift Tutorial, we have learned about Swift break statement with syntax and examples covering different scenarios.
The main rule is that break ends the nearest loop or switch immediately. Use a labeled break when nested control flow must exit a specific outer statement, and use continue instead when only the current loop iteration should be skipped.
TutorialKart.com