标签-中断vs继续vs goto [英] Labels - break vs continue vs goto

查看:122
本文介绍了标签-中断vs继续vs goto的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解:

break-停止进一步执行循环构造.

break - stops further execution of a loop construct.

continue-跳过循环主体的其余部分,并开始下一次迭代.

continue - skips the rest of the loop body and starts the next iteration.

但是与标签结合使用时,这些语句有何不同?

But how do these statements differ when used in combination with labels?

换句话说,这三个循环之间有什么区别?

In other words what is the difference between these three loops:

Loop:
    for i := 0; i < 10; i++ {
        if i == 5 {
             break Loop
        }
        fmt.Println(i)
    }


输出:


Output:

0 1 2 3 4

0 1 2 3 4

Loop:
    for i := 0; i < 10; i++ {
        if i == 5 {
             continue Loop
        }
        fmt.Println(i)
    }


输出:


Output:

0 1 2 3 4 6 7 8 9

0 1 2 3 4 6 7 8 9

Loop:
    for i := 0; i < 10; i++ {
        if i == 5 {
             goto Loop
        }
        fmt.Println(i)
    }


输出:


Output:

0 1 2 3 4 0 1 2 3 4 ...(无限)

0 1 2 3 4 0 1 2 3 4 ... (infinite)

推荐答案

对于breakcontinue,附加标签使您可以指定要引用的循环.例如,您可能想要break/continue外循环而不是嵌套的外循环.

For break and continue, the additional label lets you specify which loop you would like to refer to. For example, you may want to break/continue the outer loop instead of the one that you nested in.

以下是转到文档中的示例:

Here is an example from the Go Documentation:

RowLoop:
    for y, row := range rows {
        for x, data := range row {
            if data == endOfRow {
                continue RowLoop
            }
            row[x] = data + bias(x, y)
        }
    }

这使您可以转到下一个行",即使您当前正在行中的列(数据)上进行迭代.之所以起作用,是因为标签RowLoop通过直接在外循环之前对其进行标记".

This lets you go the the next "row" even when your currently iterating over the columns (data) in the row. This works because the label RowLoop is "labeling" the outer loop by being directly before it.

break可以以相同的方式以相同的方式使用.这是转到文档中的示例,说明当break语句可用于突破在switch语句内部的循环.没有标签,go会认为您是在退出switch语句,而不是循环(这里是您想要的).

break can be used in the same way with the same mechanics. Here is an example from the Go Documentation for when a break statement is useful for breaking out of a loop when inside of a switch statement. Without the label, go would think you were breaking out of the switch statement, instead of the loop (which is what you want, here).

OuterLoop:
    for i = 0; i < n; i++ {
        for j = 0; j < m; j++ {
            switch a[i][j] {
            case nil:
                state = Error
                break OuterLoop
            case item:
                state = Found
                break OuterLoop
            }
        }
    }

对于goto,这是比较传统的.接下来,使程序在Label处执行命令.在您的示例中,这会导致无限循环,因为您反复进入循环的开始.

For goto, this is more traditional. It makes the program execute the command at the Label, next. In your example, this leads to an infinite loop since you go to the beginning of the loop, repeatedly.

对于 break :

如果有标签,则必须是封闭的"for","switch"或"select"语句的标签,并且该标签的执行终止.

If there is a label, it must be that of an enclosing "for", "switch", or "select" statement, and that is the one whose execution terminates.

对于 continue :

如果有一个标签,则必须是一个封闭的"for"语句的标签,并且该标签将执行.

If there is a label, it must be that of an enclosing "for" statement, and that is the one whose execution advances.

这篇关于标签-中断vs继续vs goto的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆