break and continue
Let's go back to the running track. So far the runner stopped only when the laps ran out or the condition became false. But sometimes the goal is reached in the middle of the distance, and there is no point in running on. Then the runner leaves the track at once.
A loop that searches for something behaves the same way: once it has found what it needs, it should stop. The break statement ends the loop at once. This program looks for the first number above 50 that is divisible by 7:
for n in 51...100 {
if n % 7 == 0 {
print(n) // 56
break
}
}Two ways to change the course of a loop
break and continue work in any loop: in for-in, while and repeat-while.
break
break is the runner leaving the race. The lines of the body below break do not run, there are no more iterations, and the program goes on from the line after the loop.
Let's go through the example iteration by iteration:
- For
nfrom51to55the remainder of division by7is not zero, and the loop goes on. - When
nis56, the condition is true, and the program prints56. breakends the loop, and the numbers from57to100are never checked.
break sits inside the if, but it ends the whole loop, not only the if.
continue
continue is the runner cutting the rest of the current lap short and starting the next one right away. The loop does not end: for-in takes the next value, while checks its condition again, and the body starts over.
for n in 1...6 {
if n % 3 == 0 {
continue
}
print(n, terminator: " ") // 1 2 4 5
}The numbers 3 and 6 are divisible by 3, so for them continue skips the print. If you replace continue with break, the program prints only 1 2: the loop ends when n is 3.
Here the runner picture is inaccurate: a runner decides for themselves when to leave the track. break and continue act only when execution reaches their line, which is why they usually sit inside an if.
Nested loops
A loop inside another loop is called nested. On the track it is a workout of several sets: in each set the runner runs their own laps. On each iteration of the outer loop, the inner loop runs all its iterations from the start.
This is how you go through pairs of numbers. The program below looks for pairs a and b from 1 to 3 whose sum is 5:
for a in 1...3 {
for b in 1...3 {
if a + b == 5 {
print(a, b, terminator: "; ") // 2 3; 3 2;
break
}
}
}Let's see what break does:
- When
ais1, no sum equals5, and the inner loop runs all three iterations. - When
ais2, the sum2 + 3equals5: the program prints the pair, andbreakends the inner loop. - The outer loop keeps going, and when
ais3, it finds the pair3 2.
break ends only the loop whose body it is in—here, the inner one. The set is over, but the workout goes on.
A labeled break
What if you need only the first pair? Then you have to leave both loops at once. For that the outer loop gets a label—a name with a colon before the word for: search: for a in 1...3. You choose the label's name yourself: search, outer or any other. The statement break search ends the loop with that label together with all the loops inside it.
This is not the argument label from the print lesson. That one stands before a value in a function call, while a loop label stands before the loop itself and gives it a name.
Press "Next step" and watch the constants a and b: after break search both disappear, and there is no iteration with a equal to 3.
1search: for a in 1...3 {2 for b in 1...3 {3 if a + b == 5 {4 print("found", a, b)5 break search6 }7 }8}9print("done")
The outer loop starts its first iteration, and the constant a is 1.
A plain break ends the set, and a labeled break ends the whole workout. continue works with a label too: continue search goes straight to the next iteration of the outer loop.
Interviewers often ask how to leave two nested loops at once. The answer: put a label on the outer loop and write break with that label.
Common mistake
break written inside a switch that sits in a loop:
for n in 1...5 {
switch n {
case 3:
break // ✗
default:
print(n, terminator: " ")
}
}The goal was to stop the loop at three, but the program prints 1 2 4 5. Inside a switch a plain break leaves only the switch, as in an empty case, and the loop goes on. Put a label on the loop and leave by it: with numbers: for n in 1...5 and break numbers the program prints 1 2.
Key points
breakends the loop at once, andcontinueskips the rest of the current iteration and moves on to the next one.- In nested loops a plain
breakends only the loop whose body it is in. - A
breakwith the outer loop's label, such asbreak search, leaves both loops at once. - Inside a
switcha plainbreakleaves only theswitch, and a labeledbreakends the loop.