有人能告诉我break语句和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语句跳出循环(要执行的下一个语句是右大括号之后的第一个语句),而continue语句在下一个迭代中开始循环。

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的语义:

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:;

下面是continue的语义:

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:;
}

简单的程序,了解继续和中断之间的区别

当使用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

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 -终极指南