有人能告诉我break语句和continue语句的区别吗?
当前回答
Break语句
有时有必要在循环完全遍历所有步长值之前退出循环。例如,遍历一个数字列表,直到找到一个满足特定条件的数字。或者循环文件中的字符流,直到读取某个字符。
在下面的例子中,我们使用一个简单的for循环来打印从0到9的值:
for(int i=0; i<10; i++) {
System.out.println(i);
}
输出:
0
1
2
3
4
5
6
7
8
9
现在,如果我们在i==4时添加break语句,代码将在i= 4时跳出循环。可以使用break语句来中断for循环、while循环和do-while循环。break语句只会跳出当前循环。为了从嵌套的内循环中跳出外部循环,需要在break语句中使用标签。
for(int i=0; i<10; i++) {
System.out.println(i);
if(i==4) {
break;
}
}
输出:
0
1
2
3
4
继续声明
Java的continue语句跳过循环的当前迭代,直接进入下一个迭代。在for循环中调用continue语句后,循环执行将执行step值并在继续下一次迭代之前计算布尔条件。在下面的例子中,我们在循环中打印从0到9的所有值,但我们跳过打印4。
for(int i=0; i<10; i++) {
if(i==4) {
continue;
}
System.out.println(i);
}
输出:
0
1
2
3
5 <---- SKIPPED OVER 4 and continued with next loop iteration
6
7
8
9
循环标签中断语句 可以在嵌套循环中使用标签,方法是指定在跳出内部循环后继续执行的位置。通常,break语句只会跳出最内层的循环,因此当您想跳出外层循环时,可以使用标签来完成这一点,本质上是做类似于goto语句的事情。
下面的示例使用了3个循环,它们彼此嵌套。由于没有办法从最外层的循环中完全跳出最外层的循环,我们可以使用标签“outer1”来完成这一点,并在break语句旁边指定标签。
outer1:
for(int i=0; i<5; i++) {
for(int j=0; j<4; j++) {
for(int k=0; k<2; k++) {
System.out.println("[" + i + "][" + j + "][" + k + "]");
if(j == 3) {
break outer1;
}
}
}
}
输出:
[0][0][0]
[0][0][1]
[0][1][0]
[0][1][1]
[0][2][0]
[0][2][1]
[0][3][0]
注意显示的最后一行是“0[0]”,也就是j == 3的位置,这就是我们调用“break outer1;”来跳出最外层循环的位置。
循环标签-继续语句
还可以使用带有continue关键字的标签从特定的点继续循环。以前面的例子为例,只改变一行来指定continue outer1;而不是打破1;将导致循环从outer1标签开始继续循环,而不是跳出循环。注意每一次如何继续outer1;调用时,代码从外部循环开始,在循环索引I加1之后继续执行。
outer1:
for(int i=0; i<5; i++) {
for(int j=0; j<4; j++) {
for(int k=0; k<2; k++) {
System.out.println("[" + i + "][" + j + "][" + k + "]");
if(j == 3) {
continue outer1;
}
}
}
[0][0][0]
[0][0][1]
[0][1][0]
[0][1][1]
[0][2][0]
[0][2][1]
[0][3][0] <---- CONTINUE WITH LABEL CALLED HERE
[1][0][0] <---- CONTINUES FROM NEXT ITERATION OF OUTER LOOP
[1][0][1]
[1][1][0]
[1][1][1]
[1][2][0]
[1][2][1]
[1][3][0] <---- CONTINUE WITH LABEL CALLED HERE
[2][0][0] <---- CONTINUES FROM NEXT ITERATION OF OUTER LOOP
[2][0][1]
[2][1][0]
[2][1][1]
[2][2][0]
[2][2][1]
[2][3][0] <---- CONTINUE WITH LABEL CALLED HERE
[3][0][0] <---- CONTINUES FROM NEXT ITERATION OF OUTER LOOP
[3][0][1]
[3][1][0]
[3][1][1]
[3][2][0]
[3][2][1]
[3][3][0] <---- CONTINUE WITH LABEL CALLED HERE
[4][0][0] <---- CONTINUES FROM NEXT ITERATION OF OUTER LOOP
[4][0][1]
[4][1][0]
[4][1][1]
[4][2][0]
[4][2][1]
[4][3][0]
来源:循环在Java -终极指南
其他回答
你在一个for或while循环中。使用打破;就会把你排除在外。也就是说,它会结束。继续;将告诉它运行下一次迭代。
在if语句中使用continue没有意义,而是break;是有用的。 在开关……Case,总是用破;结束一个用例,使它不再执行另一个用例。
for (int i = 1; i <= 3; i++) {
if (i == 2) {
continue;
}
System.out.print("[i:" + i + "]");
在netbeans中尝试这段代码,您将理解break和continue之间的区别
for (int i = 1; i <= 3; i++) {
if (i == 2) {
break;
}
System.out.print("[i:" + i + "]");
continue跳过当前正在执行的循环并移动到下一个循环,而break移出循环并在循环后执行下一个语句。 我使用下面的代码了解了其中的区别。查看不同的输出。希望这能有所帮助。
public static void main(String[] args) {
for(int i = 0; i < 5; i++){
if (i == 3) {
continue;
}
System.out.print(i);
}
}//prints out 0124, continue moves to the next iteration skipping printing 3
public static void main(String[] args) {
for(int i = 0; i < 5; i++){
if (i == 3) {
break;
}
System.out.print(i);
}
}//prints out 012, break moves out of the loop hence doesnt print 3 and 4
Break只留下一个循环,继续跳转到下一个迭代。
首先,我认为你应该知道在Java中有两种类型的中断和继续,即有标记的中断,无标记的中断,有标记的继续和无标记的继续。现在,我将谈谈它们之间的区别。
class BreakDemo {
public static void main(String[] args) {
int[] arrayOfInts =
{ 32, 87, 3, 589,
12, 1076, 2000,
8, 622, 127 };
int searchfor = 12;
int i;
boolean foundIt = false;
for (i = 0; i < arrayOfInts.length; i++) {
if (arrayOfInts[i] == searchfor) {
foundIt = true;
break;//this is an unlabeled break,an unlabeled break statement terminates the innermost switch,for,while,do-while statement.
}
}
if (foundIt) {
System.out.println("Found " + searchfor + " at index " + i);
} else {
System.out.println(searchfor + " not in the array");
}
}
未标记的break语句终止最里面的开关、for、while、do-while语句。
public class BreakWithLabelDemo {
public static void main(String[] args) {
search:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
System.out.println(i + " - " + j);
if (j == 3)
break search;//this is an labeled break.To notice the lab which is search.
}
}
}
带标记的break终止外部语句。如果你javac和Java这个演示,你会得到:
0 - 0
0 - 1
0 - 2
0 - 3
class ContinueDemo {
public static void main(String[] args) {
String searchMe = "peter piper picked a " + "peck of pickled peppers";
int max = searchMe.length();
int numPs = 0;
for (int i = 0; i < max; i++) {
// interested only in p's
if (searchMe.charAt(i) != 'p')
continue;//this is an unlabeled continue.
// process p's
numPs++;
}
System.out.println("Found " + numPs + " p's in the string.");
}
未标记的continue语句将跳过for、while、do-while语句的当前迭代。
public class ContinueWithLabelDemo {
public static void main(String[] args) {
search:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
System.out.println(i + " - " + j);
if (j == 3)
continue search;//this is an labeled continue.Notice the lab which is search
}
}
}
一个带标签的continue语句将跳过当前使用给定标签标记的外部循环的迭代,如果你javac和java演示,你将得到:
0 - 0
0 - 1
0 - 2
0 - 3
1 - 0
1 - 1
1 - 2
1 - 3
2 - 0
2 - 1
2 - 2
2 - 3
如果你有任何问题,你可以看看这个Java教程:在这里输入链接描述
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 在Java流是peek真的只是调试?
- Recyclerview不调用onCreateViewHolder
- 将JSON字符串转换为HashMap