中断缩进循环结构

发布于 12-18 09:29 字数 265 浏览 4 评论 0原文

在诸如 for 之类的循环结构中,break; 是否“知道”何时处于缩进级别?

例如:

for (i = 0; i < 10; i++) {
    for (s = 0; s <= i; s++) {
        if (s == 7) break;
    }
}

这个 break; 会同时停止还是仅停止内部?

谢谢

In a loop structure such as for, does the break; "know" when it's under indentation levels?

For example:

for (i = 0; i < 10; i++) {
    for (s = 0; s <= i; s++) {
        if (s == 7) break;
    }
}

Will this break; stop both for or just the inner one?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

怪我太投入2024-12-25 09:29:17

你还没有说你正在使用什么语言,但我从来没有见过一种具有这种语法的语言除了打破内部循环(允许外部循环继续)之外做任何事情。不过,您确实应该提及您正在使用的语言! :-)

许多具有此类语法的语言都支持标签和定向中断,可用于中断外循环。通常看起来像这样:(

outer: for (i = 0; i < 10; i++) {
    inner: for (s = 0; s <= i; s++) {
        if (s == 7) break outer;
    }
}

在这种情况下 inner 标签是可选的,因为我们没有将它用于任何用途,我只是为了完整性而将其包含在内。)

该示例将破坏内部 < em>和外循环。它在 Java、JavaScript 以及其他可能的语言中都有效;在 C 中,你需要在循环后面有一个标签,并且(不寒而栗)有一个goto,你可能会更好(可以说总是更好),只需在外循环中包含某种终止条件检查内循环是否设置。

这是 JavaScript 中的一个实例(我使用了较小的数字以使其更加明显): 没有定向中断 | 使用定向中断

另请注意,在这些语言中,缩进是完全无关的。此代码与您的代码完全相同(只是可读性明显较差):

for (i = 0; i < 10; i++) {
for (s = 0; s <= i; s++) {
if (s == 7) break;
}
}

这也是相同的(并且可读性明显较差):

        for (i = 0; i < 10; i++) {
    for (s = 0; s <= i; s++) {
if (s == 7) break;
    }
        }

显然您' d 永远不想这样做,但重点是缩进是为人而设计的,而不是编译器(在具有这种语法风格的语言中)。

You haven't said what language you're using, but I've never seen a language with that sort of syntax that did anything other than break the inner loop (allowing the outer to continue). Still, though, you really should mention what language you're using! :-)

Many of the languages with that sort of syntax support labels and directed breaks, which can be used to break the outer loop. That usually looks like this:

outer: for (i = 0; i < 10; i++) {
    inner: for (s = 0; s <= i; s++) {
        if (s == 7) break outer;
    }
}

(The inner label is optional in this case because we haven't used it for anything, I've just included it for completeness.)

That example would break both the inner and outer loops. It's valid in Java, JavaScript, and probably others; in C you'd need a label after the loop and (shudder) a goto and you'd probably be better off (arguably are always better off) just including some kind of termination condition in the outer loop's check that the inner loop sets.

Here's a live example in JavaScript (I've used smaller numbers to make it more obvious): Without the directed break | With the directed break.

Also note that in these languages, indentation is completely irrelevant. This code is exactly the same your code (just markedly less readable):

for (i = 0; i < 10; i++) {
for (s = 0; s <= i; s++) {
if (s == 7) break;
}
}

This is also identical (and dramatically less readable):

        for (i = 0; i < 10; i++) {
    for (s = 0; s <= i; s++) {
if (s == 7) break;
    }
        }

Obviously you'd never want to do that, but the point is that indentation is for people, not compilers (in languages with this style of syntax).

好久不见√2024-12-25 09:29:17

它只会破坏内部循环。

请参阅MSDN 文档

在循环中,break 终止最近的封闭 doforwhile 语句的执行。控制权传递到终止语句后面的语句(如果有)。

It will break the inner loop only.

Refer the MSDN doccumentation.

In loops, break terminates execution of the nearest enclosing do, for, or while statement. Control passes to the statement that follows the terminated statement, if any.

晨曦慕雪2024-12-25 09:29:17

在使用荣誉的语言中,缩进级别并不重要。只有内循环才会被破坏。

Indentation levels don't matter in languages that use accolades. Only the inner loop will be broken.

梦回梦里2024-12-25 09:29:17

它只会停止内在的。

It will stop just the inner one.

铁憨憨2024-12-25 09:29:17

只是内在的那一个。 Break 将跳出单个循环。您需要额外的逻辑来完全摆脱嵌套循环,否则,如果您的嵌套循环封装在函数中,则可以使用 return 。

Just the inner one. Break will break out of a single loop. You need extra logic to break out of nested loops completely, or else you can use return if your nested loop is encapsulated in a function.

九命猫2024-12-25 09:29:17

只是内在的那一个。有些语言有一种方法来选择要破坏的语言。

Just the inner one. Some languages have a way to choose which to break.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文