For Loop
package main
import "fmt"
func main() {
// Basic for loop with a condition
i := 1
for i <= 3 {
fmt.Println(i)
i = i + 1
}
// For loop with an initialization statement, a condition, and a post statement
for j := 7; j <= 9; j++ {
fmt.Println(j)
}
// Infinite loop with a break statement
for {
fmt.Println("loop")
break
}
// For loop with continue statement to skip even numbers
for n := 0; n <= 5; n++ {
if n%2 == 0 {
continue
}
fmt.Println(n)
}
}Output
Explanation:
Last updated