Skip to content
Source Code

循环(For)

for循环是Go的唯一循环结构。这里有一些基本类型的for循环。

for is Go’s only looping construct. Here are some basic types of for loops.

最基本的类型,只有一个条件。

The most basic type, with a single condition.

go
package main

import "fmt"

func main() {
	i := 1  
	for i <= 3 {
		fmt.Println(i)
		i = i + 1     
	}
}
package main

import "fmt"

func main() {
	i := 1  
	for i <= 3 {
		fmt.Println(i)
		i = i + 1     
	}
}
经典的初始化/条件/后续操作的for循环。

A classic initial/condition/after for loop.

go
package main

import "fmt"

func main() {
	for j := 7; j <= 9; j++ { 
		fmt.Println(j)
	}
}
package main

import "fmt"

func main() {
	for j := 7; j <= 9; j++ { 
		fmt.Println(j)
	}
}
如果没有条件,for循环将会一直重复执行,直到你跳出循环或从包含该循环的函数中返回。

for without a condition will loop repeatedly until you break out of the loop or return from the enclosing function.

go
package main

import "fmt"

func main() {
	for { 
		fmt.Println("loop")
		break              
	}
}
package main

import "fmt"

func main() {
	for { 
		fmt.Println("loop")
		break              
	}
}
你还可以使用continue语句来进入下一次循环迭代。

You can also continue to the next iteration of the loop.

go
package main

import "fmt"

func main() {
	for n := 0; n <= 5; n++ { 
		if n%2 == 0 { 
			continue 
		} 
		fmt.Println(n) 
	} 
}
package main

import "fmt"

func main() {
	for n := 0; n <= 5; n++ { 
		if n%2 == 0 { 
			continue 
		} 
		fmt.Println(n) 
	} 
}
当我们查看range语句、通道和其他数据结构时,还会看到其他类型的for循环形式。

We’ll see some other for forms later when we look at range statements, channels, and other data structures.

运行

go
package main

import "fmt"

func main() {

	i := 1
	for i <= 3 {
		fmt.Println(i)
		i = i + 1
	}

	for j := 7; j <= 9; j++ {
		fmt.Println(j)
	}

	for {
		fmt.Println("loop")
		break
	}

	for n := 0; n <= 5; n++ {
		if n%2 == 0 {
			continue
		}
		fmt.Println(n)
	}
}
package main

import "fmt"

func main() {

	i := 1
	for i <= 3 {
		fmt.Println(i)
		i = i + 1
	}

	for j := 7; j <= 9; j++ {
		fmt.Println(j)
	}

	for {
		fmt.Println("loop")
		break
	}

	for n := 0; n <= 5; n++ {
		if n%2 == 0 {
			continue
		}
		fmt.Println(n)
	}
}
bash
go run 05-for.go
# 1
# 2
# 3
# 7
# 8
# 9
# loop
# 1
# 3
# 5
go run 05-for.go
# 1
# 2
# 3
# 7
# 8
# 9
# loop
# 1
# 3
# 5

Released under the MIT License.