go_playground/5-go-by-example/5-for.go

35 lines
486 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import "fmt"
// for is Gos only looping construct
func main() {
// basic type with a single condition
i := 1
for i <= 3 {
fmt.Println(i)
i = i + 1
}
// classic initial/condition/after
for j := 7; j <= 9; j++ {
fmt.Println(j)
}
// without a condition will loop repeatedly until break or return
for {
fmt.Println("loop")
break
}
// continue to the next iteration
for n := 0; n <= 5; n++ {
if n%2 == 0 {
continue
}
fmt.Println(n)
}
}