请解释标签语句的用法 [英] Please explain the usage of Labeled Statements

查看:150
本文介绍了请解释标签语句的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 是否打破并继续使用Java中标记语句的唯一用途?

  • 您何时在程序中使用了Labeled Statements?

很抱歉,代码段已被删除。我正在分裂问题

推荐答案

JLS 14.7标记语句



(为清晰起见编辑)

JLS 14.7 Labeled statements

(edited for clarity)


语句可能有标签前缀(标识符语句)。 标识符被声明为直接包含的语句的标签。

Statements may have label prefixes (Identifier : Statement). The Identifier is declared to be the label of the immediately contained Statement.

与C和C ++不同,Java编程语言没有 goto 语句;标识符语句标签与 break 一起使用(§14.15)或继续§14.16)语句出现在标签声明中的任何位置。

Unlike C and C++, the Java programming language has no goto statement; identifier statement labels are used with break (§14.15) or continue (§14.16) statements appearing anywhere within the labeled statement.

因此,JLS清楚标签与 break continue 一起使用,没有其他语法Java编程语言的元素使用它。

So the JLS is clear that labels are used with break or continue, and no other grammatical element of the Java programming language uses it.

严格来说, break 继续,无论是否标记, NEVER 都是必需的。它们总是可以写出代码。然而,在惯用语中,它们可以使代码更具可读性。

Strictly speaking, break and continue, labeled or not, are NEVER necessary. They can always be written out of the code. Used idiomatically, however, they can lead to more readable code.

这是一个说明性示例:给出 int [] ,我们希望:

Here's an illustrative example: given an int[], we want to :


  • 打印一(1) ) 1

  • print Two(2) on 2

  • print Zero on 0

  • 立即停止处理任何其他号码

  • print "One (1)" on 1
  • print "Two (2)" on 2
  • print "Zero " on 0
  • immediately stop processing on any other number

int[] arr = { 1, 2, 0, 1, -1, 0, 2 };
loop:
for (int num : arr) {
    switch (num) {
    case 1:
        System.out.print("One ");
        break;
    case 2:
        System.out.print("Two ");
        break;
    case 0:
        System.out.print("Zero ");
        continue loop;
    default:
        break loop;
    }
    System.out.print("(" + num + ") ");
}
// prints "One (1) Two (2) Zero One (1) "


我们在这里看到:


  • 处理不同的数字在开关

  • 中未标记的中断开关用于避免案件之间的堕落

  • 标签继续循环; 用于跳过案例0上的后期处理:(此处不需要标签)

  • 标签中断循环; 用于终止默认的循环:(这里需要 标签;否则它是切换中断

  • The different numbers are processed in a switch
  • Unlabeled break in the switch is used to avoid "fall-through" between cases
  • Labeled continue loop; is used to skip post-processing on case 0: (the label is not necessary here)
  • Labeled break loop; is used to terminate the loop on default: (the label is necessary here; otherwise it's a switch break)

标记为 break / continue 也可以在嵌套循环之外使用;它可以在开关嵌套在循环中时使用。更一般地说,当有可能存在多个 break / continue 目标时使用它,并且你想选择一个不是立即附上中断 / 继续声明。

So labeled break/continue can also be used outside of nested loops; it can be used when a switch is nested inside a loop. More generally, it's used when there are potentially multiple break/continue target, and you want to choose one that is not immediately enclosing the break/continue statement.

这是另一个例子:

    morningRoutine: {
        phase1: eatBreakfast();
        if (grumpy) break morningRoutine;
        phase2: kissWife();
        phase3: hugChildren();
    }
    http://stackoverflow.com is the best website ever!

这是另一个标有休息的案例不是在迭代语句中使用,而是在简单的块语句中使用。有人可能会说这些标签会带来更好的可读性;这一点是主观的。

Here's another case of a labeled break being used not within an iterative statement, but rather within a simple block statement. One may argue that the labels lead to better readability; this point is subjective.

不,最后一行不会给出编译时错误。它实际上受到了 Java Puzzlers Puzzle 22:Dupe of URL的启发。不幸的是,这个谜题并没有更深入地正确使用标记语句。

And no, the last line DOES NOT give compile time error. It's actually inspired by Java Puzzlers Puzzle 22: Dupe of URL. Unfortunately, the puzzle does not go into "proper" use of labeled statements in more depth.

这篇关于请解释标签语句的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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