Example: For Loop Go
package main
import "fmt"
func main() {
i := 1
for i <= 5 {
fmt.Println(i)
i = i + 1
}
}
Output:
1
2
3
4
5
Example: Alternate Syntax for For Loop
package main
import "fmt"
func main() {
for x := 10; x <= 15; x++ {
fmt.Println(x)
}
}
Output:
10
11
12
13
14
15
Example: Break command in For Loop
package main
import "fmt"
func main() {
for x := 10; x <= 15; x++ {
fmt.Println(x)
if x == 12 {
break
}
}
}
Output:
10
11
12
Example: Output Odd Numbers using continue command in For Loop
package main
import "fmt"
func main() {
for x := 1; x <= 10; x++ {
if x % 2 == 0 {
continue
}
fmt.Println(x)
}
}
Output:
1
3
5
7
9