Break and Continue
Explore how break and continue statements manage loop execution in Go. Understand when to skip or exit loops to optimize control flow and iteration, enhancing your programming efficiency.
We'll cover the following...
Introduction
Sometimes, we may want to skip the execution of a loop for a certain condition or terminate it immediately without checking the condition. To specifically change the flow of execution, we have another two statements, break and continue.
break statement
In every iteration, a condition has to be checked to see whether the loop should stop. If the exit-condition becomes true, the loop is left through the break statement. A break statement always breaks out of the innermost structure in which it occurs; it can be used in any for-loop (counter, condition, and so on), but also in a switch, or a select statement. Execution is continued after the ending } of that structure.
The following figure explains the break statement.
Run the following program to understand ...