这不应该无限循环吗?
someLabel:{
for (int i = 0; i < 5; ++i) {
System.out.println(i);
if (i == 3)
break someLabel;
}
}
开始了
0
1
2
3
然后死了。我正在阅读有关 Java 中 GOTO 替代方法的问题。这是公认的答案:https://stackoverflow.com/a/2430789/555690 . for循环是不是应该再执行一次?
最佳答案
来自 http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html :
The break statement terminates the labeled statement; it does not transfer the flow of control to the label. Control flow is transferred to the statement immediately following the labeled (terminated) statement.
除了我从未见过任何使用这种控制流的真实代码之外,我猜它的用途是从内循环中断开外循环(如提供的链接中所述):
search:
for (i = 0; i < arrayOfInts.length; i++) {
for (j = 0; j < arrayOfInts[i].length;
j++) {
if (arrayOfInts[i][j] == searchfor) {
foundIt = true;
break search;
}
}
}
关于java - 关于 Java 中的标签表现得像 GOTO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24543955/