Lesson 5

Mastering Loop Control with Break and Continue in Go

Introduction

Today, we are going to learn about break and continue, which are control tools used in loops in Go. The break command allows us to exit a loop early, while continue facilitates skipping unnecessary iterations. Let's dive in!

Break Statement

You can liken the break command to the moment when the music stops in a game of musical chairs, prompting you to leave the loop. It ends the loop, irrespective of the original condition of the loop.

Here is a quick example:

Go
1for i := 0; i < 10; i++ { 2 if i == 7 { // When `i` reaches 7 3 fmt.Println("7 found! Break!") // Message before the break 4 break // Terminating the loop 5 } 6 fmt.Println("Number:", i) // Print i until we hit "break" 7} 8// Prints: 9// Number: 0 10// Number: 1 11// Number: 2 12// Number: 3 13// Number: 4 14// Number: 5 15// Number: 6 16// 7 found! Break!

Our loop operates on numbers from 0 through 6 and breaks when it reaches 7, thereby exiting early and skipping all remaining iterations.

Continue Statement

The continue keyword in Go can be compared to bypassing a boring view during a walk. It disregards the current loop iteration and moves ahead to the next one.

Here is an example:

Go
1for j := 1; j <= 10; j++ { 2 if j == 4 || j == 7 { // Skip the 4th and 7th buildings 3 continue 4 } 5 fmt.Println("Admiring building number:", j) // Continue with the rest 6} 7// Prints: 8// Admiring building number: 1 9// Admiring building number: 2 10// Admiring building number: 3 11// Admiring building number: 5 12// Admiring building number: 6 13// Admiring building number: 8 14// Admiring building number: 9 15// Admiring building number: 10

Our output confirms that we admire all buildings except numbers 4 and 7, which our continue statement skips.

Break and Continue in Nested Loops

Nested loops are loops within loops. In these loops, break and continue work in distinct ways. It's important to understand that both break and continue will exit or skip only their respective inner loop, not affecting the outer loop. Let's illustrate this with a couple of examples.

Consider a nested loop running on a 5x5 grid.

Go
1for i := 1; i <= 5; i++ { 2 fmt.Print(i, ": ") 3 for j := 1; j <= 5; j++ { 4 if i == 3 && j == 3 { 5 // break the inner loop 6 break 7 } 8 fmt.Print(j, " ") 9 } 10 fmt.Println() 11} 12// Prints: 13// 1: 1 2 3 4 5 14// 2: 1 2 3 4 5 15// 3: 1 2 16// 4: 1 2 3 4 5 17// 5: 1 2 3 4 5

In this context, break ends the inner loop when i and j both equal 3. Thus, when i becomes 3, the inner loop runs only up to j = 2 and then terminates. However, the outer loop continues until i = 5.

Meanwhile, let's introduce 'continue' in a similar setup.

Go
1for i := 1; i <= 5; i++ { 2 fmt.Print(i, ": ") 3 for j := 1; j <= 5; j++ { 4 if i == 3 && j == 3 { 5 continue 6 } 7 fmt.Print(j, " ") 8 } 9 fmt.Println() 10} 11// Prints: 12// 1: 1 2 3 4 5 13// 2: 1 2 3 4 5 14// 3: 1 2 4 5 15// 4: 1 2 3 4 5 16// 5: 1 2 3 4 5

When continue encounters the i = 3, j = 3 condition, it skips the rest of the code inside its loop and swiftly moves to the next iteration. In this case, it means we omit printing j when both i and j are equal to 3.

Lesson Summary and Practice

Bravo! You've learned about the break and continue commands in Go, giving you greater control over loops. As practice is crucial to mastering these concepts, let's immerse ourselves in some exercises to bolster your newfound skills. Enjoy your practice session!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.