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

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

问题描述

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

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.

我个人以前只使用过一次,为了在其他两个for循环中使用if-else打破for循环并继续在外循环中,如<$循环中的c $ c> 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天全站免登陆