Loop control statements alter the normal execution flow of loops and other statements. They allow a program to terminate a loop early, skip the current iteration, or transfer control to another labeled statement.
Go supports three loop control statements:
- Break
- Goto
- Continue
Example: The following code demonstrates how break and continue can control the execution flow inside a loop.
package main
import "fmt"
func main() {
for i := 1; i <= 5; i++ {
if i == 3 {
continue
}
if i == 5 {
break
}
fmt.Println(i)
}
}
Output
1 2 4
Break Statement
The break statement immediately terminates the loop or switch statement in which it appears. Program execution then continues with the first statement after the terminated block.
Note: In nested loops, break only exits the innermost loop containing the statement.
Example:
package main
import "fmt"
func main() {
for i := 0; i < 5; i++ {
fmt.Println(i)
if i == 3 {
break
}
}
}
Output
0 1 2 3
Explanation:
- The for loop iterates from 0 to 4.
- fmt.Println(i) prints the current value of i.
- When i == 3, break terminates the loop.
- Execution then continues with the statements after the loop.
Flow Chart:

Continue Statement
The continue statement skips the remaining statements in the current iteration and transfers control to the next iteration of the loop.
Example:
package main
import "fmt"
func main() {
for i := 0; i < 8; i++ {
if i == 5 {
continue
}
fmt.Println(i)
}
}
Output
0 1 2 3 4 6 7
Explanation:
- The loop iterates from 0 to 7.
- When i becomes 5, continue skips the remaining statements in that iteration.
- 5 is not printed.
- The loop then proceeds to the next iteration.
Flow Chart:

Goto Statement
The goto statement transfers program execution to a labeled statement. A label is an identifier followed by a colon (:) and must be defined within the same function.
Note: Although goto is supported, its usage is generally discouraged because it can make code difficult to read and maintain. Structured loops with break and continue are preferred in most situations.
Example:
package main
import "fmt"
func main() {
x := 0
Label:
if x == 5 {
return
}
fmt.Println(x)
x++
goto Label
}
Output
0 1 2 3 4
Explanation:
- Label defines a target location in the program.
- goto Label transfers execution back to the labeled statement.
- The program repeatedly prints values from 0 to 4.
- When x becomes 5, return terminates the program.
Flow Chart:
