xxxxxxxxxx
package main
import "fmt"
func main() {
sum := 0
for i := 0; i < 10; i++ {
sum += i
}
fmt.Println(sum)
}
xxxxxxxxxx
// Program to print the first 5 natural numbers
package main
import "fmt"
func main() {
// for loop terminates when i becomes 6
for i := 1; i <= 5; i++ {
fmt.Println(i)
}
}
xxxxxxxxxx
sum := 0
for i := 1; i < 5; i++ {
sum += i
}
fmt.Println(sum) // 10 (1+2+3+4)
xxxxxxxxxx
import (
"fmt"
)
func main() {
numbs := []int{1, 2, 3, 4, 5}
for numb := range numbs {
fmt.Printf("using range: %d \n", numb)
}
fmt.Println("\n")
for i := 0; i < len(numbs); i++ {
fmt.Printf("using for: %d \n", i)
}
fmt.Println("\n")
i := 0
for i < len(numbs) {
fmt.Printf("using for like while: %d \n", i)
i++
}
}
xxxxxxxxxx
for count := 0; count <= 10; count++ {
fmt.Println("My counter is at", count)
}
xxxxxxxxxx
// Program to print the sum of natural numbers: 1 + 2 + 3 + ... +n
package main
import "fmt"
func main() {
var n, sum = 10, 0
for i := 1 ; i <= n; i++ {
sum += i // sum = sum + i
}
fmt.Println("sum =", sum)
}
xxxxxxxxxx
// Program to print the first 5 natural numbers
package main
import "fmt"
func main() {
// for loop terminates when i becomes 6
for i := 1; i <= 5; i++ {
fmt.Println(i)
}
}