break和continue语句之间的差异。

时间:2022-09-06 13:13:10

Can anyone tell me the difference between break and continue statements?

有人能告诉我break和continue的区别吗?

21 个解决方案

#1


413  

break leaves a loop, continue jumps to the next iteration.

中断留下一个循环,继续跳到下一个迭代。

#2


92  

See Branching Statements for more details and code samples:

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

break

The break statement has two forms: labeled and unlabeled. You saw the unlabeled form in the previous discussion of the switch statement. You can also use an unlabeled break to terminate a for, while, or do-while loop [...]

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

An unlabeled break statement terminates the innermost switch, for, while, or do-while statement, but a labeled break terminates an outer statement.

一个未标记的break语句终止最里面的开关,在while或do-while语句中,但是有标记的中断终止一个外部语句。

continue

The continue statement skips the current iteration of a for, while , or do-while loop. The unlabeled form skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop. [...]

continue语句跳过当前迭代的for、while或do-while循环。未标记的表单会跳到最内循环体的末尾,并计算控制循环的布尔表达式。[…]

A labeled continue statement skips the current iteration of an outer loop marked with the given label.

标记的continue语句跳过标记为给定标签的外部循环的当前迭代。

#3


54  

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

This will lead to following output:

这将导致以下输出:

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

You can label a block, not only a for-loop, and then break/continue from a nested block to an outer one. In few cases this might be useful, but in general you'll try to avoid such code, except the logic of the program is much better to understand than in the following example:

您可以标记一个块,不只是一个for循环,然后从一个嵌套的块到一个外部的块。在少数情况下,这可能是有用的,但是一般来说,您会尽量避免使用这种代码,除非程序的逻辑比下面的例子更好理解:

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

Because it's possible, it doesn't mean you should use it.

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

If you want to obfuscate your code in a funny way, you don't choose a meanigful name, but http: and follow it with a comment, which looks alien, like a webadress in the source-code:

如果你想以一种有趣的方式来混淆你的代码,你不会选择一个卑鄙的名字,而是选择http:然后加上一条评论,看起来很陌生,就像源代码中的网络地址:

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

I guess this is from a Joshua Bloch quizzle. :)

我猜这是来自约书亚·布洛赫的一声细语。:)

#4


26  

Break leaves the loop completely and executes the statements after the loop. Whereas Continue leaves the current iteration and executes with the next value in the loop.

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

This Code Explains Everything :

这段代码解释了一切:

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");
    }
}

Output:

输出:

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

#5


21  

break completely exits the loop. continue skips the statements after the continue statement and keeps looping.

完全退出循环。在继续语句之后继续跳过语句并保持循环。

#6


7  

Excellent answer simple and accurate.

优秀的回答简单而准确。

I would add a code sample.

我将添加一个代码示例。

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

#7


6  

A break statement results in the termination of the statement to which it applies (switch, for, do, or while).

break语句导致语句的终止(switch、for、do或while)。

A continue statement is used to end the current loop iteration and return control to the loop statement.

使用continue语句结束当前循环迭代,并将控制返回到loop语句。

#8


5  

continue skips the current executing loop and MOVES TO the next loop whereas break MOVES OUT of the loop and executes the next statement after the loop. I learned the difference using the following code. Check out the different outputs.Hope this helps.

继续跳过当前执行循环并移动到下一个循环,而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

#9


4  

Consider the following:

考虑以下:

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

break causes the loop to terminate and the value of n is 0.

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

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

continue causes the program counter to return to the first line of the loop (the condition is checked and the value of n is increment) and the final value of n is 10.

继续使程序计数器返回到循环的第一行(条件被检查,n的值为增量),n的最终值为10。

It should also be noted that break only terminates the execution of the loop it is within:

应该注意的是,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);

Will output something to the effect of

会产生什么效果吗?

0
0
0
0
0
5

#10


3  

The break statement breaks out of the loop (the next statement to be executed is the first one after the closing brace), while continue starts the loop over at the next iteration.

break语句从循环中跳出(下一个要执行的语句是关闭括号后的第一个语句),而继续在下一次迭代中开始循环。

#11


2  

To prevent anything from execution if a condition is met one should use the continue and to get out of the loop if a condition is met one should use the break.

如果条件满足,则应使用continue并从循环中退出,以防止任何情况发生,如果遇到条件,则应使用break。

For example in the below mentioned code.

例如下面提到的代码。

 for(int i=0;i<5;i++){

        if(i==3){

           continue;

        }
       System.out.println(i);
     }

The above code will print the result : 0 1 2 4

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

NOw consider this code

现在考虑这个代码

 for(int i=0;i<5;i++){


            if(i==3){

                break;

            }
            System.out.println(i);
         }

This code will print 0 1 2

此代码将打印0 1 2。

That is the basic difference in the continue and break.

这就是继续和中断的基本区别。

#12


1  

The break statement exists the current looping control structure and jumps behind it while the continue exits too but jumping back to the looping condition.

break语句存在当前的循环控制结构,并在继续退出时跳到它后面,但又返回到循环状态。

#13


1  

here's the semantic of break:

下面是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:;

here's the semantic of 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:;
}

#14


1  

First,i think you should know that there are two types of break and continue in Java which are labeled break,unlabeled break,labeled continue and unlabeled continue.Now, i will talk about the difference between them.

首先,我认为您应该知道,在Java中有两种类型的break和continue,它们被标记为break,没有标记的break,标记为continue和unlabel 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");
    }
}

An unlabeled break statement terminates the innermost switch ,for ,while ,do-while statement.

一个未标记的break语句终止最里面的开关,因为,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.
        }
    }
}

A labeled break terminates an outer statement.if you javac and java this demo,you will get:

有标记的断点终止外部语句。如果你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.");
}

An unlabeled continue statement skips the current iteration of a for,while,do-while statement.

未标记的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
        }
    }
}

A labeled continue statement skips the current iteration of an outer loop marked with the given lable,if you javac and java the demo,you will get:

一个被标记的continue语句跳过了一个标记为给定lable的外部循环的当前迭代,如果您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

if you have any question , you can see the Java tutorial of this:enter link description here

如果您有任何问题,您可以看到这个Java教程:在这里输入链接描述。

#15


1  

Simple Example:

简单的例子:

break leaves the loop.

打破循环。

int m = 0;
for(int n = 0; n < 5; ++n){
  if(n == 2){
    break;
  }
  m++;
}

System.out.printl("m:"+m); // m:2

continue will go back to start loop.

继续返回开始循环。

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

#16


0  

Simply put: break will terminate the current loop, and continue execution at the first line after the loop ends. continue jumps back to the loop condition and keeps running the loop.

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

#17


0  

for (int i = 1; i <= 3; i++) {
        if (i == 2) {

            continue;
        }
        System.out.print("[i:" + i + "]");

try this code in netbeans you'll understand the different between break and continue

在netbeans中尝试此代码,您将了解break和continue之间的不同。

for (int i = 1; i <= 3; i++) {
        if (i == 2) {

            break;
        }
        System.out.print("[i:" + i + "]");

#18


0  

Simple program to understand difference between continue and break

简单的程序来理解继续和中断之间的区别。

When continue is used

继续使用时

    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

When break is used

当使用打破

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

#19


0  

Continue Statment stop the itration and start next ittration Ex:

继续状态停止it定量并开始下一个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

中断状态停止循环或退出循环。

#20


0  

Break Statement

Break语句

Sometimes it’s necessary to exit a loop before the loop has finished fully iterating over all the step values. For example, looping over a list of numbers until you find a number that satisfies a certain condition. Or looping over a stream of characters from a file until a certain character is read.

有时,在循环完成遍历所有步骤值之前,需要退出循环。例如,循环遍历一个数字列表,直到找到满足某个条件的数字。或从文件中循环一个字符流,直到读取某个字符为止。

In the following example, we’re using a simple for loop to print out values from 0 to 9:

在下面的示例中,我们使用一个简单的for循环将0到9的值打印出来:

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

Output:

输出:

0
1
2
3
4
5
6
7
8
9

Now if we add a break statement when i==4, our code will break out of the loop once i equals 4. You can use the break statement to break out of for loops, while loops and do-while loops. The break statement will only break out of the current loop. In order to break out of an outer loop from a nested inner loop, you would need to use labels with the break statement.

现在,如果我们在==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;
  }
}

Output:

输出:

0
1
2
3
4

Continue Statement

继续声明

Java’s continue statement skips over the current iteration of a loop and goes directly to the next iteration. After calling the continue statement in a for loop, the loop execution will execute the step value and evaluate the boolean condition before proceeding with the next iteration. In the following example, we’re printing out all values from 0 to 9 in a loop but we skip over printing out 4.

Java的continue语句跳过循环的当前迭代,直接进入下一个迭代。在for循环中调用continue语句之后,循环执行将执行步骤值,并在进行下一次迭代之前评估布尔条件。在下面的例子中,我们将所有的值从0输出到9,但是我们跳过了打印4。

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

Output:

输出:

0
1
2
3
5 <---- SKIPPED OVER 4 and continued with next loop iteration
6
7
8
9

Loop Label - Break Statement You can use labels within nested loops by specifying where you want execution to continue after breaking out of an inner loop. Normally, the break statement will only break out of the innermost loop so when you want to break out of an outer loop, you can use labels to accomplish this, essentially doing something similar to a goto statement.

循环标签- Break语句,你可以在嵌套循环中使用标签,指定你想要执行的地方,在跳出一个内部循环之后。通常,break语句只会从最里面的循环中跳出来,所以当你想要跳出一个外部循环时,你可以使用标签来完成这个,本质上是做一些类似于goto语句的事情。

The following example uses 3 loops, all nested within each other. Since there’s no way completely break out of the outer most loop from inside the inner most loop, we can use the label “outer1” to accomplish this and specify the label next to the break statement.

下面的示例使用3个循环,它们都嵌套在一起。由于没有办法完全从内部的大部分循环中跳出外部的大多数循环,我们可以使用label“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;
      }
    }
  }
}

Output:

输出:

[0][0][0]
[0][0][1]
[0][1][0]
[0][1][1]
[0][2][0]
[0][2][1]
[0][3][0]

Notice how the last line displayed is “0[0]” which is where j == 3 and that’s where we called “break outer1;” to break out of the outer most loop.

请注意最后一行显示的是“0[0]”,即j == 3,这就是我们所说的“break outer1”,它是从外部的大多数循环中跳出来的。

Loop Labels - Continue Statement

循环标签——继续声明。

You can also use labels with the continue keyword to continue looping from a specific point. Taking the previous example and just changing one line to specify continue outer1; instead of break outer1; will cause the loop to continue looping from the outer1 label instead of breaking out of the loop. Note how each time continue outer1; is called, the code continues from the outer loop after incrementing the loop index i by 1.

您还可以使用continue关键字的标签继续从特定的点循环。以前面的例子为例,仅更改一行来指定continue outer1;而不是打破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]

Source: Loops in Java – Ultimate Guide

源:Java -终极指南中的循环。

#21


-1  

so you are inside a for or while loop. Using break; will put you outside of the loop. As in, it will end. Continue; will tell it to run the next iteration.

所以你在一个for或while循环中。使用打破;会让你跳出循环。就像在里面一样,它会结束。继续;将告诉它运行下一个迭代。

No point in using continue in if statement, but break; is useful. In switch...case, always use break; to end a case, so it does not executes another case.

如果语句没有继续使用,但是中断;是有用的。在开关……情况下,总是使用打破;为了结束一个案例,所以它不执行另一个案例。

#1


413  

break leaves a loop, continue jumps to the next iteration.

中断留下一个循环,继续跳到下一个迭代。

#2


92  

See Branching Statements for more details and code samples:

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

break

The break statement has two forms: labeled and unlabeled. You saw the unlabeled form in the previous discussion of the switch statement. You can also use an unlabeled break to terminate a for, while, or do-while loop [...]

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

An unlabeled break statement terminates the innermost switch, for, while, or do-while statement, but a labeled break terminates an outer statement.

一个未标记的break语句终止最里面的开关,在while或do-while语句中,但是有标记的中断终止一个外部语句。

continue

The continue statement skips the current iteration of a for, while , or do-while loop. The unlabeled form skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop. [...]

continue语句跳过当前迭代的for、while或do-while循环。未标记的表单会跳到最内循环体的末尾,并计算控制循环的布尔表达式。[…]

A labeled continue statement skips the current iteration of an outer loop marked with the given label.

标记的continue语句跳过标记为给定标签的外部循环的当前迭代。

#3


54  

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

This will lead to following output:

这将导致以下输出:

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

You can label a block, not only a for-loop, and then break/continue from a nested block to an outer one. In few cases this might be useful, but in general you'll try to avoid such code, except the logic of the program is much better to understand than in the following example:

您可以标记一个块,不只是一个for循环,然后从一个嵌套的块到一个外部的块。在少数情况下,这可能是有用的,但是一般来说,您会尽量避免使用这种代码,除非程序的逻辑比下面的例子更好理解:

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

Because it's possible, it doesn't mean you should use it.

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

If you want to obfuscate your code in a funny way, you don't choose a meanigful name, but http: and follow it with a comment, which looks alien, like a webadress in the source-code:

如果你想以一种有趣的方式来混淆你的代码,你不会选择一个卑鄙的名字,而是选择http:然后加上一条评论,看起来很陌生,就像源代码中的网络地址:

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

I guess this is from a Joshua Bloch quizzle. :)

我猜这是来自约书亚·布洛赫的一声细语。:)

#4


26  

Break leaves the loop completely and executes the statements after the loop. Whereas Continue leaves the current iteration and executes with the next value in the loop.

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

This Code Explains Everything :

这段代码解释了一切:

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");
    }
}

Output:

输出:

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

#5


21  

break completely exits the loop. continue skips the statements after the continue statement and keeps looping.

完全退出循环。在继续语句之后继续跳过语句并保持循环。

#6


7  

Excellent answer simple and accurate.

优秀的回答简单而准确。

I would add a code sample.

我将添加一个代码示例。

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

#7


6  

A break statement results in the termination of the statement to which it applies (switch, for, do, or while).

break语句导致语句的终止(switch、for、do或while)。

A continue statement is used to end the current loop iteration and return control to the loop statement.

使用continue语句结束当前循环迭代,并将控制返回到loop语句。

#8


5  

continue skips the current executing loop and MOVES TO the next loop whereas break MOVES OUT of the loop and executes the next statement after the loop. I learned the difference using the following code. Check out the different outputs.Hope this helps.

继续跳过当前执行循环并移动到下一个循环,而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

#9


4  

Consider the following:

考虑以下:

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

break causes the loop to terminate and the value of n is 0.

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

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

continue causes the program counter to return to the first line of the loop (the condition is checked and the value of n is increment) and the final value of n is 10.

继续使程序计数器返回到循环的第一行(条件被检查,n的值为增量),n的最终值为10。

It should also be noted that break only terminates the execution of the loop it is within:

应该注意的是,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);

Will output something to the effect of

会产生什么效果吗?

0
0
0
0
0
5

#10


3  

The break statement breaks out of the loop (the next statement to be executed is the first one after the closing brace), while continue starts the loop over at the next iteration.

break语句从循环中跳出(下一个要执行的语句是关闭括号后的第一个语句),而继续在下一次迭代中开始循环。

#11


2  

To prevent anything from execution if a condition is met one should use the continue and to get out of the loop if a condition is met one should use the break.

如果条件满足,则应使用continue并从循环中退出,以防止任何情况发生,如果遇到条件,则应使用break。

For example in the below mentioned code.

例如下面提到的代码。

 for(int i=0;i<5;i++){

        if(i==3){

           continue;

        }
       System.out.println(i);
     }

The above code will print the result : 0 1 2 4

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

NOw consider this code

现在考虑这个代码

 for(int i=0;i<5;i++){


            if(i==3){

                break;

            }
            System.out.println(i);
         }

This code will print 0 1 2

此代码将打印0 1 2。

That is the basic difference in the continue and break.

这就是继续和中断的基本区别。

#12


1  

The break statement exists the current looping control structure and jumps behind it while the continue exits too but jumping back to the looping condition.

break语句存在当前的循环控制结构,并在继续退出时跳到它后面,但又返回到循环状态。

#13


1  

here's the semantic of break:

下面是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:;

here's the semantic of 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:;
}

#14


1  

First,i think you should know that there are two types of break and continue in Java which are labeled break,unlabeled break,labeled continue and unlabeled continue.Now, i will talk about the difference between them.

首先,我认为您应该知道,在Java中有两种类型的break和continue,它们被标记为break,没有标记的break,标记为continue和unlabel 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");
    }
}

An unlabeled break statement terminates the innermost switch ,for ,while ,do-while statement.

一个未标记的break语句终止最里面的开关,因为,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.
        }
    }
}

A labeled break terminates an outer statement.if you javac and java this demo,you will get:

有标记的断点终止外部语句。如果你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.");
}

An unlabeled continue statement skips the current iteration of a for,while,do-while statement.

未标记的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
        }
    }
}

A labeled continue statement skips the current iteration of an outer loop marked with the given lable,if you javac and java the demo,you will get:

一个被标记的continue语句跳过了一个标记为给定lable的外部循环的当前迭代,如果您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

if you have any question , you can see the Java tutorial of this:enter link description here

如果您有任何问题,您可以看到这个Java教程:在这里输入链接描述。

#15


1  

Simple Example:

简单的例子:

break leaves the loop.

打破循环。

int m = 0;
for(int n = 0; n < 5; ++n){
  if(n == 2){
    break;
  }
  m++;
}

System.out.printl("m:"+m); // m:2

continue will go back to start loop.

继续返回开始循环。

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

#16


0  

Simply put: break will terminate the current loop, and continue execution at the first line after the loop ends. continue jumps back to the loop condition and keeps running the loop.

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

#17


0  

for (int i = 1; i <= 3; i++) {
        if (i == 2) {

            continue;
        }
        System.out.print("[i:" + i + "]");

try this code in netbeans you'll understand the different between break and continue

在netbeans中尝试此代码,您将了解break和continue之间的不同。

for (int i = 1; i <= 3; i++) {
        if (i == 2) {

            break;
        }
        System.out.print("[i:" + i + "]");

#18


0  

Simple program to understand difference between continue and break

简单的程序来理解继续和中断之间的区别。

When continue is used

继续使用时

    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

When break is used

当使用打破

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

#19


0  

Continue Statment stop the itration and start next ittration Ex:

继续状态停止it定量并开始下一个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

中断状态停止循环或退出循环。

#20


0  

Break Statement

Break语句

Sometimes it’s necessary to exit a loop before the loop has finished fully iterating over all the step values. For example, looping over a list of numbers until you find a number that satisfies a certain condition. Or looping over a stream of characters from a file until a certain character is read.

有时,在循环完成遍历所有步骤值之前,需要退出循环。例如,循环遍历一个数字列表,直到找到满足某个条件的数字。或从文件中循环一个字符流,直到读取某个字符为止。

In the following example, we’re using a simple for loop to print out values from 0 to 9:

在下面的示例中,我们使用一个简单的for循环将0到9的值打印出来:

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

Output:

输出:

0
1
2
3
4
5
6
7
8
9

Now if we add a break statement when i==4, our code will break out of the loop once i equals 4. You can use the break statement to break out of for loops, while loops and do-while loops. The break statement will only break out of the current loop. In order to break out of an outer loop from a nested inner loop, you would need to use labels with the break statement.

现在,如果我们在==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;
  }
}

Output:

输出:

0
1
2
3
4

Continue Statement

继续声明

Java’s continue statement skips over the current iteration of a loop and goes directly to the next iteration. After calling the continue statement in a for loop, the loop execution will execute the step value and evaluate the boolean condition before proceeding with the next iteration. In the following example, we’re printing out all values from 0 to 9 in a loop but we skip over printing out 4.

Java的continue语句跳过循环的当前迭代,直接进入下一个迭代。在for循环中调用continue语句之后,循环执行将执行步骤值,并在进行下一次迭代之前评估布尔条件。在下面的例子中,我们将所有的值从0输出到9,但是我们跳过了打印4。

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

Output:

输出:

0
1
2
3
5 <---- SKIPPED OVER 4 and continued with next loop iteration
6
7
8
9

Loop Label - Break Statement You can use labels within nested loops by specifying where you want execution to continue after breaking out of an inner loop. Normally, the break statement will only break out of the innermost loop so when you want to break out of an outer loop, you can use labels to accomplish this, essentially doing something similar to a goto statement.

循环标签- Break语句,你可以在嵌套循环中使用标签,指定你想要执行的地方,在跳出一个内部循环之后。通常,break语句只会从最里面的循环中跳出来,所以当你想要跳出一个外部循环时,你可以使用标签来完成这个,本质上是做一些类似于goto语句的事情。

The following example uses 3 loops, all nested within each other. Since there’s no way completely break out of the outer most loop from inside the inner most loop, we can use the label “outer1” to accomplish this and specify the label next to the break statement.

下面的示例使用3个循环,它们都嵌套在一起。由于没有办法完全从内部的大部分循环中跳出外部的大多数循环,我们可以使用label“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;
      }
    }
  }
}

Output:

输出:

[0][0][0]
[0][0][1]
[0][1][0]
[0][1][1]
[0][2][0]
[0][2][1]
[0][3][0]

Notice how the last line displayed is “0[0]” which is where j == 3 and that’s where we called “break outer1;” to break out of the outer most loop.

请注意最后一行显示的是“0[0]”,即j == 3,这就是我们所说的“break outer1”,它是从外部的大多数循环中跳出来的。

Loop Labels - Continue Statement

循环标签——继续声明。

You can also use labels with the continue keyword to continue looping from a specific point. Taking the previous example and just changing one line to specify continue outer1; instead of break outer1; will cause the loop to continue looping from the outer1 label instead of breaking out of the loop. Note how each time continue outer1; is called, the code continues from the outer loop after incrementing the loop index i by 1.

您还可以使用continue关键字的标签继续从特定的点循环。以前面的例子为例,仅更改一行来指定continue outer1;而不是打破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]

Source: Loops in Java – Ultimate Guide

源:Java -终极指南中的循环。

#21


-1  

so you are inside a for or while loop. Using break; will put you outside of the loop. As in, it will end. Continue; will tell it to run the next iteration.

所以你在一个for或while循环中。使用打破;会让你跳出循环。就像在里面一样,它会结束。继续;将告诉它运行下一个迭代。

No point in using continue in if statement, but break; is useful. In switch...case, always use break; to end a case, so it does not executes another case.

如果语句没有继续使用,但是中断;是有用的。在开关……情况下,总是使用打破;为了结束一个案例,所以它不执行另一个案例。