有人能告诉我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;

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

其他回答

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

当使用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语句跳出循环(要执行的下一个语句是右大括号之后的第一个语句),而continue语句在下一个迭代中开始循环。

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

这段代码解释了一切:

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

考虑以下几点:

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

Continue使程序计数器返回到循环的第一行(检查条件,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