可以在for循环之外适当地使用java标签吗? [英] Can java labels be used appropriately outside of for loops?

查看:141
本文介绍了可以在for循环之外适当地使用java标签吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我见过很多情况,标签被用于for循环,特别是双重嵌套循环。



这是唯一的情况他们可以在哪里使用?还是有其他常见的用途,我只是不知道。我觉得它是一个我从来没有用过的java工具!

解决方案

这是公平的说,标签循环是唯一的>有用的方式可以使用带标签的语句。


$ b $ 允许在Java中标记任何语句,但它们的唯一使用方式是标记为 break continue 语句的目标。



通常的建议适用于:只是因为你可以做一些事情并不意味着你应该。不要使用标签作为有趣的评论(是的,我已经看到了这一点)。不要编写双重嵌套的循环,只是为了炫耀你打破或继续外部循环的能力。现在,我们倾向于支持适用于整个序列的操作,减轻标记为 break s和 continue s的某些用法。一如既往,旨在提高可读性和简单性。也就是说,即使人类程序员很少这样做,类似于解析器生成器的输出Java源代码的工具也很有可能大量使用带标签的语句。


So I've seen a lot of cases where labels are being used in for loops to break out, especially with doubly nested loops.

Is this the only case where they can be used? or are there other common uses that I just don't know about. I feel like its a java tool I've never used!

解决方案

It is fair to say that labeling a loop is the only useful way that a labeled statement can be used.

You are allowed to label any statement in Java, but the only way they are used is as a target of a (labeled) break or continue statement.

The usual advice applies though: just because you can do something does not mean you should. Don't use labels as funny "comments" (yes I have seen this done). Don't write doubly nested loops just to show off your ability to break or continue an outer loop. These days we tend to favor operations that apply to a whole sequence, mitigating some uses for labeled breaks and continues. As always, aim for readability and simplicity. That said, tools like parser generators that output Java source are quite likely to make heavy use of labeled statements, even if human programmers rarely do.

这篇关于可以在for循环之外适当地使用java标签吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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