Skip to content

Instantly share code, notes, and snippets.

@sugilog sugilog/sample.go
Created Dec 28, 2014

Embed
What would you like to do?
forループ
package main
import "fmt"
func main() {
patternA()
patternB()
patternC()
patternD()
}
func patternA() {
sum := 0
for i := 0; i < 10; i++ {
sum += i
}
fmt.Println( sum )
}
func patternB() {
sum := 0
i := 0
for ; i < 10; {
sum += i
i++
}
fmt.Println( sum )
}
func patternC() {
sum := 0
i := 0
for i < 10 {
sum += i
i++
}
fmt.Println( sum )
}
func patternD() {
sum := 0
i := 0
for {
sum += i
i++
if i >= 10 {
break
}
}
fmt.Println( sum )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.