有人能告诉我break语句和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循环,然后从一个嵌套块到一个外部块。在少数情况下,这可能是有用的,但一般情况下,你会尽量避免这样的代码,除非程序的逻辑比下面的例子更好理解:

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

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

如果你想以一种有趣的方式混淆你的代码,你不选择一个有意义的名字,而是http:,并在它后面加上一个注释,看起来很陌生,就像源代码中的网络地址:

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

我猜这是约书亚·布洛赫做的小测验。:)

其他回答

简单地说: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
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循环,然后从一个嵌套块到一个外部块。在少数情况下,这可能是有用的,但一般情况下,你会尽量避免这样的代码,除非程序的逻辑比下面的例子更好理解:

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

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

如果你想以一种有趣的方式混淆你的代码,你不选择一个有意义的名字,而是http:,并在它后面加上一个注释,看起来很陌生,就像源代码中的网络地址:

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

我猜这是约书亚·布洛赫做的小测验。:)

首先,我认为你应该知道在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教程:在这里输入链接描述

你在一个for或while循环中。使用打破;就会把你排除在外。也就是说,它会结束。继续;将告诉它运行下一次迭代。

在if语句中使用continue没有意义,而是break;是有用的。 在开关……Case,总是用破;结束一个用例,使它不再执行另一个用例。