Go > Core Go Basics > Control Flow > Break and continue
Break and Continue Statements in Go
This snippet demonstrates the usage of break and continue statements within loops in Go, showcasing how they alter the normal flow of execution.
Basic Break Example
This code demonstrates the break statement. The loop iterates from 0 to 9. When i equals 5, the break statement is executed, causing the loop to terminate immediately. The program then proceeds to print "Loop finished."
package main
import "fmt"
func main() {
for i := 0; i < 10; i++ {
if i == 5 {
break // Exit the loop when i is 5
}
fmt.Println(i)
}
fmt.Println("Loop finished.")
}
Basic Continue Example
This example shows the continue statement. The loop iterates from 0 to 9. When i is even (i.e., i%2 == 0 is true), the continue statement is executed. This causes the current iteration to be skipped, and the loop proceeds to the next iteration. Only odd numbers are printed.
package main
import "fmt"
func main() {
for i := 0; i < 10; i++ {
if i%2 == 0 {
continue // Skip even numbers
}
fmt.Println(i)
}
fmt.Println("Loop finished.")
}
Concepts behind the Snippets
break and continue are control flow statements used to alter the execution of loops. break terminates the loop prematurely, whereas continue skips the current iteration and proceeds with the next one.
Real-Life Use Case
break can be used to exit a loop when a specific condition is met, such as finding a particular element in a list. continue can be used to skip processing certain elements in a loop based on specific criteria, such as filtering out invalid data.
Best Practices
Use break and continue judiciously. Overuse can make code harder to read and understand. Always ensure the logic behind these statements is clear and well-documented.
Interview Tip
Be prepared to explain the difference between break and continue, and provide examples of when each would be appropriately used. Common interview questions involve tracing code execution with these statements.
When to use them
Use break when you need to completely exit a loop based on a specific condition. Use continue when you need to skip the current iteration of a loop but continue processing the remaining iterations.
Alternatives
In some cases, you can avoid using break and continue by restructuring your loop condition or using boolean flags. However, these alternatives can sometimes make the code more complex.
Pros
break and continue can improve code readability and efficiency by providing a concise way to control loop execution.
Cons
Overuse of break and continue can lead to spaghetti code that is difficult to understand and maintain. It can make the control flow less obvious.
FAQ
-
What is the difference between
breakandcontinue?
breakterminates the loop entirely, whilecontinueonly skips the current iteration. -
Can I use
breakandcontinueoutside of loops?
No,breakandcontinueare only valid within loops (for,while(simulated with for in Go),switch). Using them outside of a loop will result in a compilation error.