By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
By default a loop runs from start to finish. Two keywords change that from inside the body: break leaves the loop, and continue skips the rest of the current pass. They are opposites in a useful way, and both belong inside an if so the loop keeps its usual behaviour when the special case does not apply.
break ends the innermost loop immediately. The step is not run and the condition is not checked; execution continues at the first statement after the loop. Use it when the answer is found and more passes would be wasted work.
package main
import "fmt"
func main() {
cents := []int{450, 1250, 300, 5000}
for _, amount := range cents {
fmt.Println("checking", amount)
if amount > 1000 {
fmt.Println("found", amount)
break
}
}
fmt.Println("search over")
}The loop stops at the first match, and the amounts after it are never looked at. For a search that is exactly right: once one match is enough, checking the rest is wasted work.
continue jumps on to the next pass. The rest of the body is skipped, then the step statement runs and the condition is checked again. It is a clean way to say not this one, try the next. Note the difference: break leaves the loop, while continue stays in it.
package main
import "fmt"
func main() {
total := 0
for i := 1; i <= 6; i++ {
if i%2 == 0 {
continue
}
total += i
}
fmt.Println("sum of odd numbers:", total)
}The even numbers are skipped before the addition, so they never contribute to the total. You could write the same loop with the condition i%2 != 0 in the header, and either version is fine. Choose the one that makes the intent clearer to a reader.
A loop inside a loop runs the inner loop completely for every single pass of the outer loop. Nested loops are the natural tool for shapes and grids: rows and columns, triangles, tables. A triangle appears because the inner loop's condition depends on the outer loop's counter.
package main
import "fmt"
func main() {
for row := 1; row <= 4; row++ {
for star := 1; star <= row; star++ {
fmt.Print("*")
}
fmt.Println()
}
}For row 1 the inner loop runs once, for row 2 twice, and so on, so each row is one star longer than the row above. fmt.Print writes without adding a newline, and fmt.Println() with no arguments ends the row. Trace it once on paper; after that the shape reads easily.
FizzBuzz is the classic loop exercise. Count from 1 to 15. For each number, print Fizz when it is divisible by 3, Buzz when it is divisible by 5, and FizzBuzz when it is divisible by both. This program uses the whole section so far: a for loop, the remainder operator, switch, and printing.
package main
import "fmt"
func main() {
for n := 1; n <= 15; n++ {
switch {
case n%15 == 0:
fmt.Println("FizzBuzz")
case n%3 == 0:
fmt.Println("Fizz")
case n%5 == 0:
fmt.Println("Buzz")
default:
fmt.Println(n)
}
}
}Check the order of the cases: the divisible-by-15 case comes first, because 15 is also divisible by 3 and by 5. Put the Fizz test first and every 15 would print Fizz alone. The same lesson from step 10 shows up in a program you will recognise for the rest of your career.
A plain break inside two nested loops leaves only the inner loop. If you need to leave both at once, label the outer loop and name it in the break statement.
Try to design nested loops so the extra exit is not needed. A small helper function is usually clearer than a label, and section 4 gives you functions and returns.
Write a countdown from 10 to 1 that skips 7 with continue and prints liftoff after the loop, without using break. Then print a square of stars: 4 rows with 4 stars each, using two nested loops. Finally, reverse the triangle from the lesson so the first row has 4 stars and the last row has 1, by starting the star counter at 4 and stopping it when it is less than the row number. Run each program and check the shape.
Key takeaways
break leaves the innermost loop at once; continue skips to the next pass.
A nested loop runs the inner loop completely for every pass of the outer loop.
Triangles and grids come from an inner loop whose limit depends on the outer counter.
FizzBuzz combines a loop, the remainder operator and switch; test the rarest case first.
Labels let break and continue target an outer loop when one loop is not enough.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.