Loops are an essential part of programming that allow executing a block of code multiple times. In Go, the only loop available is the for
loop, but it can be used in different ways to achieve the functionality of while
and do-while
loops found in other languages.
for
Loopfor initialization; condition; increment {
// Code to execute
}
đš Key Points:
â
Initialization: Variable is initialized before the loop starts.
â
Condition: The loop runs as long as the condition is true
.
â
Increment/Decrement: Controls how the loop variable updates.
for
Loop Examplepackage main
import "fmt"
func main() {
for i := 1; i <= 5; i++ {
fmt.Println("Iteration:", i)
}
}
đš Output:
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
đ Explanation:
i := 1
â The loop starts with i = 1
.i <= 5
â The loop runs as long as i
is ⤠5
.i++
â i
is incremented by 1
after each iteration.for
as a While LoopIn Go, the for
loop can also be used like a while
loop by omitting the initialization and increment parts.
package main
import "fmt"
func main() {
i := 1
for i <= 5 {
fmt.Println("Value of i:", i)
i++
}
}
đš Output:
Value of i: 1
Value of i: 2
Value of i: 3
Value of i: 4
Value of i: 5
đ Explanation:
for
â i
is declared before the loop.for
â i++
is manually written inside the loop.for
without condition)If a for
loop has no condition, it runs forever unless explicitly stopped with break
.
package main
import "fmt"
func main() {
i := 1
for {
fmt.Println("Running... Iteration:", i)
i++
if i > 5 {
break
}
}
}
đš Output:
Running... Iteration: 1
Running... Iteration: 2
Running... Iteration: 3
Running... Iteration: 4
Running... Iteration: 5
đ Explanation:
if i > 5 { break }
â Stops the loop when i > 5
.break
to Exit a LoopThe break
statement is used to exit a loop immediately, regardless of the condition.
package main
import "fmt"
func main() {
for i := 1; i <= 10; i++ {
if i == 5 {
fmt.Println("Stopping loop at:", i)
break
}
fmt.Println("Iteration:", i)
}
}
đš Output:
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Stopping loop at: 5
đ Explanation:
i == 5
, the break
statement stops the loop.continue
to Skip an IterationThe continue
statement skips the current iteration and moves to the next one.
package main
import "fmt"
func main() {
for i := 1; i <= 5; i++ {
if i == 3 {
fmt.Println("Skipping iteration:", i)
continue
}
fmt.Println("Iteration:", i)
}
}
đš Output:
Iteration: 1
Iteration: 2
Skipping iteration: 3
Iteration: 4
Iteration: 5
đ Explanation:
i == 3
, continue
skips the iteration, and the loop moves to i = 4
.A nested loop is a loop inside another loop.
package main
import "fmt"
func main() {
for i := 1; i <= 3; i++ {
for j := 1; j <= 3; j++ {
fmt.Printf("(%d, %d) ", i, j)
}
fmt.Println()
}
}
đš Output:
(1, 1) (1, 2) (1, 3)
(2, 1) (2, 2) (2, 3)
(3, 1) (3, 2) (3, 3)
đ Explanation:
i
) runs 3 times â Controls rows.j
) runs 3 times per row â Controls columns.for range
Go provides the range
keyword to iterate over slices, arrays, and maps.
package main
import "fmt"
func main() {
nums := []int{10, 20, 30, 40, 50}
for index, value := range nums {
fmt.Printf("Index: %d, Value: %d\n", index, value)
}
}
đš Output:
Index: 0, Value: 10
Index: 1, Value: 20
Index: 2, Value: 30
Index: 3, Value: 40
Index: 4, Value: 50
đ Explanation:
index
â Position in the slice.value
â Value at that position.đš If only values are needed, _
is used to ignore index:
for _, value := range nums {
fmt.Println(value)
}
Loop Type | Description |
---|---|
for | Standard loop with initialization, condition, and increment. |
for condition {} | Behaves like a while loop. |
for {} | Infinite loop (requires break to stop). |
break | Exits the loop immediately. |
continue | Skips the current iteration. |
for range | Iterates over arrays, slices, maps, and strings. |
đ Go only has the for
loop, but it is powerful enough to replace while
and do-while
loops!
āϞā§āĻĒ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰ⧠āĻāĻāĻāĻŋ āĻā§āĻĄ āĻŦā§āϞāĻ āĻŦāĻžāϰāĻŦāĻžāϰ āĻāĻžāϞāĻžāύ⧠āϝāĻžāϝāĻŧ, āϝāϤāĻā§āώāĻŖ āύāĻž āύāĻŋāϰā§āĻĻāĻŋāώā§āĻ āĻļāϰā§āϤ āĻŽāĻŋāĻĨā§āϝāĻž āĻšāϝāĻŧāĨ¤
for
āϞā§āĻĒfor i := 1; i <= 5; i++ {
fmt.Println(i)
}
â
while
āϞā§āĻĒā§āϰ āĻŽāϤ āĻŦā§āϝāĻŦāĻšāĻžāϰ
i := 1
for i <= 5 {
fmt.Println(i)
i++
}
â
āĻāύāĻĢāĻŋāύāĻŋāĻāĻŋ āϞā§āĻĒ (for {}
)
for {
fmt.Println("āĻāϞāϤ⧠āĻĨāĻžāĻāĻŦā§...")
}
â
break
āĻāĻŦāĻ continue
for i := 1; i <= 5; i++ {
if i == 3 {
continue // 3 āĻŦāĻžāĻĻ āĻĻā§āĻŦā§
}
fmt.Println(i)
}