Concept Flow - Break statement
Start Loop
Check Condition
Yes
Execute Loop Body
Check Break Condition
Break Loop
End Loop
The loop starts and runs while the condition is true. Inside, if the break condition is met, the loop stops immediately.
package main import "fmt" func main() { for i := 1; i <= 5; i++ { if i == 3 { break } fmt.Println(i) } }
| Iteration | i | Condition i <= 5 | Break Condition i == 3 | Action | Output |
|---|---|---|---|---|---|
| 1 | 1 | true | false | Print 1 | 1 |
| 2 | 2 | true | false | Print 2 | 2 |
| 3 | 3 | true | true | Break loop | |
| 4 | - | Loop exited | - | - | - |
| Variable | Start | After 1 | After 2 | After 3 (break) | Final |
|---|---|---|---|---|---|
| i | - | 1 | 2 | 3 | 3 |
Break statement in Go:
- Used inside loops to stop the loop immediately.
- Syntax: break
- When break runs, loop exits even if condition is true.
- Useful to stop early based on a condition.
- Example: if i == 3 { break } stops loop when i is 3.break statement do inside a loop in Go?break statement is used to exit a loop immediately, stopping any further iterations.continue which skips to the next iteration, break stops the loop entirely and moves on.break inside a for loop in Go?break is the keyword used to exit a loop early.break correctly inside an if statement within the for loop.for i := 1; i <= 5; i++ {
if i == 3 {
break
}
fmt.Print(i, " ")
}break stops the loop immediately.break:for i := 0; i < 5; i++ {
if i == 2 {
break 2
}
fmt.Println(i)
}break does not accept a number to break multiple loops; it only breaks the closest loop.break 2 causes a syntax error because Go does not support labeled breaks with numbers.break to exit only the inner loop in Go?for i := 1; i <= 3; i++ {
for j := 1; j <= 3; j++ {
if j == 2 {
break
}
fmt.Printf("%d,%d ", i, j)
}
}break inside the inner loop stops only that inner loop when j == 2.Pass the test and this pattern moves unknown