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


当前回答

break语句会导致它所应用的语句(switch、for、do或while)的终止。

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

其他回答

Break完全退出循环。Continue跳过Continue语句后面的语句并继续循环。

break语句会导致它所应用的语句(switch、for、do或while)的终止。

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

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

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

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

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

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