介绍 (Introduction)
Using for loops in Go allow you to automate and repeat tasks in an efficient manner.
在Go中使用for循环可让您高效地自动化和重复执行任务。
Learning how to control the operation and flow of loops will allow for customized logic in your program. You can control your loops with the break
and continue
statements.
学习如何控制循环的操作和流程将允许您在程序中使用自定义逻辑。 您可以使用break
和continue
语句控制循环。
违约声明 (Break Statement)
In Go, the break
statement terminates execution of the current loop. A break
is almost always paired with a conditional if
statement.
在Go中, break
语句终止当前循环的执行。 break
几乎总是与条件if
语句配对。
Let’s look at an example that uses the break
statement in a for
loop:
让我们看一个在for
循环中使用break
语句的示例:
package main
import "fmt"
func main() {
for i := 0; i < 10; i++ {
if i == 5 {
fmt.Println("Breaking out of loop")
break // break here
}
fmt.Println("The value of i is", i)
}
fmt.Println("Exiting program")
}
This small program creates a for
loop that will iterate while i
is less than 10
.
这个小程序创建了一个for
循环,当i
小于10
时,它将循环进行。
Within the for
loop, there is an if
statement. The if
statement tests the condition of i
to see if the value is less than 5
. If the value of i
is not equal to 5
, the loop continues and prints out the value of i
. If the value of i
is equal to 5
, the loop will execute the break
statement, print that it is Breaking out of loop
, and stop executing the loop. At the end of the program we print out Exiting program
to signify that we have exited the loop.
在for
循环中,有一个if
语句。 if
语句测试i
的条件,以查看该值是否小于5
。 如果i
的值不等于5
,则循环继续并打印出i
的值。 如果i
的值等于5
,则循环将执行break
语句,打印出它正在Breaking out of loop
,然后停止执行循环。 在程序末尾,我们输出Exiting program
以表明我们已经退出循环。
When we run this code, our output will be the following:
运行此代码时,输出如下:
Output
The value of i is 0
The value of i is 1
The value of i is 2
The value of i is 3
The value of i is 4
Breaking out of loop
Exiting program
This shows that once the integer i
is evaluated as equivalent to 5, the loop breaks, as the program is told to do so with the break
statement.
这表明,一旦整数i
的值等于5,就中断了循环,因为该程序是使用break
语句告知这样做的。
嵌套循环 (Nested Loops)
It is important to remember that the break
statement will only stop the execution of the inner most loop it is called in. If you have a nested set of loops, you will need a break for each loop if desired.
重要的是要记住, break
语句只会停止调用它的最里面的循环的执行。如果您有一组嵌套的循环,则需要时每个循环都需要一个break。
package main
import "fmt"
func main() {
for outer := 0; outer < 5; outer++ {
if outer == 3 {
fmt.Println("Breaking out of outer loop")
break // break here
}
fmt.Println("The value of outer is", outer)
for inner := 0; inner < 5; inner++ {
if inner == 2 {
fmt.Println("Breaking out of inner loop")
break // break here
}
fmt.Println("The value of inner is", inner)
}
}
fmt.Println("Exiting program")
}
In this program, we have two loops. While both loops iterate 5 times, each has a conditional if
statement with a break
statement. The outer loop will break if the value of outer
equals 3
. The inner loop will break if the value of inner
is 2
.
在此程序中,我们有两个循环。 虽然两个循环都重复5次,但每个循环都有一个带break
语句的条件if
语句。 如果值外环将打破outer
等于3
。 如果inner
值为2
则内部循环将中断。
If we run the program, we can see the output:
如果运行程序,则可以看到输出:
Output
The value of outer is 0
The value of inner is 0
The value of inner is 1
Breaking out of inner loop
The value of outer is 1
The value of inner is 0
The value of inner is 1
Breaking out of inner loop
The value of outer is 2
The value of inner is 0
The value of inner is 1
Breaking out of inner loop
Breaking out of outer loop
Exiting program
Notice that each time the inner loop breaks, the outer loop does not break. This is because break
will only break the inner most loop it is called from.
请注意,每次内部循环中断时,外部循环都不会中断。 这是因为break
只会中断调用它的最内层循环。
We have seen how using break
will stop the execution of a loop. Next, let’s look at how we can continue the iteration of a loop.
我们已经看到了使用break
如何停止循环的执行。 接下来,让我们看看如何继续循环的迭代。
继续声明 (Continue Statement)
The continue
statement is used when you want to skip the remaining portion of the loop, and return to the top of the loop and continue a new iteration.
当您想跳过循环的其余部分,并返回到循环的顶部并继续新的迭代时,可以使用continue
语句。
As with the break
statement, the continue
statement is commonly used with a conditional if
statement.
与break
语句一样, continue
语句通常与条件if
语句一起使用。
Using the same for
loop program as in the preceding Break Statement section, we’ll use a continue
statement rather than a break
statement:
使用与前面的Break Statement部分相同的for
循环程序,我们将使用continue
语句而不是break
语句:
package main
import "fmt"
func main() {
for i := 0; i < 10; i++ {
if i == 5 {
fmt.Println("Continuing loop")
continue // break here
}
fmt.Println("The value of i is", i)
}
fmt.Println("Exiting program")
}
The difference in using the continue
statement rather than a break
statement is that our code will continue despite the disruption when the variable i
is evaluated as equivalent to 5
. Let’s look at our output:
使用continue
语句而不是break
语句的区别在于,即使变量i
的值等于5
,我们的代码也会继续运行,尽管会中断。 让我们看一下输出:
Output
The value of i is 0
The value of i is 1
The value of i is 2
The value of i is 3
The value of i is 4
Continuing loop
The value of i is 6
The value of i is 7
The value of i is 8
The value of i is 9
Exiting program
Here we see that the line The value of i is 5
never occurs in the output, but the loop continues after that point to print lines for the numbers 6-10 before leaving the loop.
在这里,我们看到The value of i is 5
从不出现在输出中,但是在该点之后循环继续进行,以在退出循环之前打印数字6-10的行。
You can use the continue
statement to avoid deeply nested conditional code, or to optimize a loop by eliminating frequently occurring cases that you would like to reject.
您可以使用continue
语句来避免深度嵌套的条件代码,或者通过消除要拒绝的频繁发生的情况来优化循环。
The continue
statement causes a program to skip certain factors that come up within a loop, but then continue through the rest of the loop.
continue
语句使程序跳过循环中出现的某些因素,然后继续执行循环的其余部分。
结论 (Conclusion)
The break
and continue
statements in Go will allow you to use for
loops more effectively in your code.
Go中的break
和continue
语句将使您可以在代码中更有效地使用for
循环。