break and continue
In the previous lesson, the runner ran laps until the condition said "enough". But sometimes the runner stops earlier: say, they are looking for a friend in the stands and leave the track the moment they spot them. There is no point in running on.
A loop that searches for something should also stop as soon as it finds it. Otherwise it runs to the end and checks numbers that are no longer needed. The break statement ends a loop at once. This program finds the first number greater than 50 that is divisible by 7:
package main
import "fmt"
func main() {
for i := 51; i <= 100; i++ {
if i%7 == 0 {
fmt.Println(i) // 56
break
}
}
}Two ways to cut a pass short
Inside a loop body, two statements change the usual run of iterations. Both go on a line of their own, most often inside an if. The difference is what they cut short.
break
break ends the whole loop. Let's go through the iterations of the program above:
- For
51–55, the conditioni%7 == 0is false, and the loop goes on. - At
56the condition is true, and the program prints56. breaktakes the runner off the track: the loop is over.
After break, the rest of the body and all later iterations are skipped. The program goes on from the line after the loop. Without break, this program would print every such number up to 100: 56, 63, 70 and so on up to 98.
Where break shows up most often:
- in a search loop, to stop at the first find;
- in an infinite loop
for { … }, where it is the usual way out once the goal is reached.
If one loop sits in the body of another, break ends only the inner one, the one whose body holds it.
continue
The continue statement skips only the rest of the current pass and moves on to the next iteration. The loop does not end. The runner cuts the rest of the lap and starts the next one, but the mark still goes into the notebook. In a three-part loop, the post statement runs after continue, and then the condition is checked.
for i := 1; i <= 6; i++ {
if i%2 == 0 {
continue
}
fmt.Print(i, " ") // 1 3 5
}Unlike Println, fmt.Print adds no space between a number and a string, so the string " " is the separator. Let's see what happens:
- For
2,4and6, the conditioni%2 == 0is true, andfmt.Printis never reached. - The other numbers are printed:
1 3 5. - The loop still runs up to
6, as it would withoutcontinue.
Replace continue with break, and the program prints only 1: the loop ends when i equals 2. continue is handy when unsuitable values should be skipped and the rest processed.
Here the notebook picture ends. A condition-only loop has no post statement in its header, so nobody makes the mark for you. There, change the variable before continue, or the next pass starts with the same value and the loop never ends.
Common mistake
A break inside a switch that sits in a loop:
for i := 1; i <= 5; i++ {
switch i {
case 3:
break // ✗
}
fmt.Print(i)
}The program prints 12345: inside a switch, break ends only the switch, and the loop goes on. Leaving the loop takes two steps:
- Put a label—a name followed by a colon, such as
loop:—in front of the loop. - Write that name after
break:break loop.
loop:
for i := 1; i <= 5; i++ {
switch i {
case 3:
break loop
}
fmt.Print(i) // 12
}Now, when i equals 3, break loop ends the whole loop, and the program prints 12. A label also lets you leave two nested loops at once: break loop in the inner loop ends the outer one too, the one the label stands before. Interviewers sometimes ask exactly this: how to leave a loop from inside a switch, or two loops at once.
Key points
breakends a loop at once, and the program goes on after it.continueskips the rest of the pass, and a three-part loop still runs its post statement.- Inside a
switch,breakends only theswitch; to leave the loop, use a label:break loop.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.