为循环命名 [英] Giving name to a loop

查看:36
本文介绍了为循环命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个论坛中浏览问题和答案时,我发现一段代码被赋予了循环名称,以便将它们用于中断.喜欢

While browsing questions and answers in this forum i found a piece of code were names were given to loops in order to use them for break. Like

nameofloop:
    for(){
        for(){
             if(){ break nameofloop;}
        }
    }

我是编程新手,以前从未见过.我的问题是命名循环还有哪些其他用途?

Im new to programming and i havent seen that before. My question is what other uses of naming loops are there?

推荐答案

这不是一个带标签的循环,它只是一个你放置在任何地方然后你可以根据你的条件中断"或继续"的标签.您还可以在嵌套的 if-else 中使用 for 循环来中断多个用 if-else 装饰的循环,这样您就可以避免设置大量标志并在 if-else 中测试它们以便在此嵌套中继续或不继续等级.

This is not a labeled loop, is just a label that you place anywhere and then you can "break" or "continue" to depending on your conditions. You can also use in a nested if-else with for loopings in order to break several loops decorated with if-else, so you can avoid setting lot of flags and testing them in the if-else in order to continue or not in this nested level.

不鼓励使用它,因为它类似于 goto 并导致意大利面条式代码.

Its use is discouraged as resembles a goto and causes spaghetti-code.

我个人只使用过一次,前一段时间,为了用 if-else 中断其他两个 for 循环内的 for 循环并在外循环中继续,因为循环内的 break 会中断这个循环,但您继续在外循环中,而不是我的情况下的最外循环.

Personally I used only once, time ago, in order to break a for loop inside other two for loops with if-else and continue in the outer loop, as break inside a loop breaks this loop, but you continue in the outer loop, not the most-outer that was my case.

这篇关于为循环命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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