在Go中使用循环时使用Break和Continue语句

介绍 (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.

学习如何控制循环的操作和流程将允许您在程序中使用自定义逻辑。 您可以使用breakcontinue语句控制循环。

违约声明 (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语句的示例:

break.go
break.go
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。

nested.go
nested.go
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语句:

continue.go
继续
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中的breakcontinue语句将使您可以在代码中更有效地使用for循环。

翻译自: https://www.digitalocean.com/community/tutorials/using-break-and-continue-statements-when-working-with-loops-in-go

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值