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


当前回答

考虑以下几点:

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

其他回答

break语句跳出循环(要执行的下一个语句是右大括号之后的第一个语句),而continue语句在下一个迭代中开始循环。

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

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

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

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

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

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