rest和继续之间的区别

任何人都可以告诉我breakcontinue语句之间的区别吗?

break一个循环, continue跳转到下一个迭代。

有关更多详细信息和代码示例,请参阅分支语句

break

break语句有两种forms:带标签和无标签。 您在前面关于switch语句的讨论中看到了无标签的表单。 您也可以使用未标记的中断来终止for,while或do-while循环[…]

未标记的break语句终止最内层的switch,for,while或do-while语句,但带标签的break将终止外部语句。

continue

continue语句跳过了for,while或do-while循环的当前迭代。 无标签的forms跳到最内层循环体的末尾,并计算控制循环的布尔expression式。 […]

带标号的continue语句跳过标有给定标签的外循环的当前迭代。

 System.out.println ("starting loop:"); for (int n = 0; n < 7; ++n) { System.out.println ("in loop: " + n); if (n == 2) { continue; } System.out.println (" survived first guard"); if (n == 4) { break; } System.out.println (" survived second guard"); // continue at head of loop } // break out of loop System.out.println ("end of loop or exit via break"); 

这将导致以下输出:

 starting loop: in loop: 0 survived first guard survived second guard in loop: 1 survived first guard survived second guard in loop: 2 in loop: 3 survived first guard survived second guard in loop: 4 survived first guard end of loop or exit via break 

您可以标记一个块,不仅是一个for循环,然后从一个嵌套块中断/继续。 在less数情况下,这可能是有用的,但总的来说,你会尽量避免这样的代码,除了程序的逻辑比下面的例子更好理解:

 first: for (int i = 0; i < 4; ++i) { second: for (int j = 0; j < 4; ++j) { third: for (int k = 0; k < 4; ++k) { System.out.println ("inner start: i+j+k " + (i + j + k)); if (i + j + k == 5) continue third; if (i + j + k == 7) continue second; if (i + j + k == 8) break second; if (i + j + k == 9) break first; System.out.println ("inner stop: i+j+k " + (i + j + k)); } } } 

因为这是可能的,这并不意味着你应该使用它。

如果你想用一个有趣的方式来混淆你的代码,你不要select一个有意义的名字,而是selecthttp:并且跟随一个看起来很陌生的注释,就像源代码中的一个webadress:

 http://stackoverflow.com/questions/462373 for (int i = 0; i < 4; ++i) { if (i == 2) break http; 

我想这是来自Joshua Bloch quizzle。 🙂

Break完全离开循环,并在循环之后执行语句。 而继续离开当前的迭代,并执行循环中的下一个值。

本规范解释了一切:

 public static void main(String[] args) { for(int i=0;i<10;i++) { if (i==4) { break; } System.out.print(i+"\t"); } System.out.println(); for(int i=0;i<10;i++) { if (i==4) { continue; } System.out.print(i+"\t"); } } 

输出:

 0 1 2 3 0 1 2 3 5 6 7 8 9 

完全退出循环。 continue跳过continue语句之后的语句并保持循环。

优秀的答案简单而准确。

我会添加一个代码示例。

 C:\oreyes\samples\java\breakcontinue>type BreakContinue.java class BreakContinue { public static void main( String [] args ) { for( int i = 0 ; i < 10 ; i++ ) { if( i % 2 == 0) { // if pair, will jump continue; // don't go to "System.out.print" below. } System.out.println("The number is " + i ); if( i == 7 ) { break; // will end the execution, 8,9 wont be processed } } } } C:\oreyes\samples\java\breakcontinue>java BreakContinue The number is 1 The number is 3 The number is 5 The number is 7 

break语句导致它所应用的语句的终止( switchfordo ,或while )。

continue语句用于结束当前的循环迭代并将控制权返回给循环语句。

continue跳过当前正在执行的循环,然后移到下一个循环,break MOVES OUT 循环,并在循环之后执行下一个语句。 我使用下面的代码了解了不同之处。 看看不同的输出。希望这有助于。

 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 

考虑以下几点:

 int n; for(n = 0; n < 10; ++n) { break; } System.out.println(n); 

break导致循环终止, n的值为0。

 int n; for(n = 0; n < 10; ++n) { continue; } System.out.println(n); 

继续导致程序计数器返回到循环的第一行(条件被检查并且n的值是递增的)并且n的最终值是10。

还应该注意的是, break只能终止它所在循环的执行:

 int m; for(m = 0; m < 5; ++m) { int n; for(n = 0; n < 5; ++n) { break; } System.out.println(n); } System.out.println(m); 

会输出一些东西的效果

 0 0 0 0 0 5 

break语句跳出循环(下一个要执行的语句是右括号之后的第一个语句),while continue在下一次循环中开始循环。

break语句存在当前的循环控制结构并在跳转后跳转,而continue退出,但跳回循环条件。

这里是break的语义:

 int[] a = new int[] { 1, 3, 4, 6, 7, 9, 10 }; // find 9 for(int i = 0; i < a.Length; i++) { if (a[i] == 9) goto goBreak; Console.WriteLine(a[i].ToString()); } goBreak:; 

这里是继续的语义:

 int[] a = new int[] { 1, 3, 4, 6, 7, 9, 10 }; // skip all odds for(int i = 0; i < a.Length; i++) { if (a[i] % 2 == 1) goto goContinue; Console.WriteLine(a[i].ToString()); goContinue:; } 

首先,我想你应该知道在Java中有两种types的break和continue,分别是break,unlabeled break,continue和unlabeled continue。现在我将讨论它们之间的区别。

 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语句终止最内层的switch,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. } } } 

带有标签的中断会终止外部语句。如果您使用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教程: 在这里input链接描述

简单的例子:

break离开循环。

 int m = 0; for(int n = 0; n < 5; ++n){ if(n == 2){ break; } m++; } System.out.printl("m:"+m); // m:2 

continue回去开始循环。

 int m = 0; for(int n = 0; n < 5; ++n){ if(n == 2){ continue; // Go back to start and dont execute m++ } m++; } System.out.printl("m:"+m); // m:4 

如果遇到条件,为了防止执行任何事情,应该使用continue并在条件满足的情况下离开循环,应该使用break。

例如在下面提到的代码。

  for(int i=0;i<5;i++){ if(i==3){ continue; } System.out.println(i); } 

以上代码将打印结果:0 1 2 4

NOw考虑这个代码

  for(int i=0;i<5;i++){ if(i==3){ break; } System.out.println(i); } 

此代码将打印0 1 2

这是继续和突破的根本区别。

简单地说:break将终止当前循环,并在循环结束后的第一行继续执行。 继续跳回循环条件并继续运行循环。

 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 + "]"); 

简单的程序来了解继续和rest的区别

continue使用

  public static void main(String[] args) { System.out.println("HelloWorld"); for (int i = 0; i < 5; i++){ System.out.println("Start For loop i = " + i); if(i==2){ System.out.println("Inside if Statement for i = "+i); continue; } System.out.println("End For loop i = " + i); } System.out.println("Completely out of For loop"); } OutPut: HelloWorld Start For loop i = 0 End For loop i = 0 Start For loop i = 1 End For loop i = 1 Start For loop i = 2 Inside if Statement for i = 2 Start For loop i = 3 End For loop i = 3 Start For loop i = 4 End For loop i = 4 Completely out of For loop 

当使用break

 public static void main(String[] args) { System.out.println("HelloWorld"); for (int i = 0; i < 5; i++){ System.out.println("Start For loop i = " + i); if(i==2){ System.out.println("Inside if Statement for i = "+i); break; } System.out.println("End For loop i = " + i); } System.out.println("Completely out of For loop"); } Output: HelloWorld Start For loop i = 0 End For loop i = 0 Start For loop i = 1 End For loop i = 1 Start For loop i = 2 Inside if Statement for i = 2 Completely out of For loop 
 Continue Statment stop the itration and start next ittration Ex: System.out.println("continue when i is 2:"); for (int i = 1; i <= 3; i++) { if (i == 2) { System.out.print("[continue]"); continue; } System.out.print("[i:" + i + "]"); } and Break Statment stop the loop or Exit from the loop 

所以你在一个for或while循环中。 使用rest; 会把你放在循环之外。 如在,它会结束。 继续; 会告诉它运行下一个迭代。

没有点使用继续在if语句,但打破; 是有用的。 在切换…的情况下,总是使用中断; 结束案件,所以它不会执行另一个案件。