📜  Java中的Break and Continue语句

📅  最后修改于: 2022-05-13 01:54:27.970000             🧑  作者: Mango

Java中的Break and Continue语句

break 和 continue 语句是跳转语句,用于跳过循环内的某些语句或立即终止循环而不检查测试表达式。这些语句可以在任何循环中使用,例如 for、while、do-while 循环。

Break Java中的 break 语句用于立即终止循环。当在循环中遇到 break 语句时,循环迭代停止,并且控制立即从循环返回到循环之后的第一个语句。基本上,当我们不确定循环的实际迭代次数,或者我们想根据某些条件终止循环时,会使用 break 语句。

句法 :

break;

在Java中,break 语句主要用于:

  • 退出循环。
  • 用作 goto 的“文明”形式。
  • 在 switch 语句中终止序列。

使用 break 退出循环

使用 break,我们可以强制立即终止循环,绕过条件表达式和循环体中的任何剩余代码。当我们在嵌套循环中使用 break 时,它只会跳出最里面的循环。

例子:

Java
// Java program to demonstrate using
// break to exit a loop
class GFG {
    public static void main(String[] args)
    {
        // Initially loop is set to run from 0-9
        for (int i = 0; i < 10; i++) {
            // Terminate the loop when i is 5
            if (i == 5)
                break;
            System.out.println("i: " + i);
        }
        System.out.println("Out of Loop");
    }
}


Java
// Java program to demonstrates using break with goto
class GFG {
    public static void main(String args[])
    {
    // First label
    first:
        for (int i = 0; i < 3; i++) {
        // Second label
        second:
            for (int j = 0; j < 3; j++) {
                if (i == 1 && j == 1) {
 
                    // Using break statement with label
                    break first;
                }
                System.out.println(i + " " + j);
            }
        }
    }
}


Java
// Java program to demonstrate using break to terminate a
// sequence in a switch statement.
class GFG {
    public static void main(String args[])
    {
        int i = 2;
        switch (i) {
        case 0:
            System.out.println("i is zero.");
            break;
        case 1:
            System.out.println("i is one.");
            break;
        case 2:
            System.out.println("i is two.");
            break;
        default:
            System.out.println("Invalid number");
        }
    }
}


Java
// Java program to demonstrates the continue
// statement to continue a loop
class GFG {
    public static void main(String args[])
    {
        for (int i = 0; i < 10; i++) {
            // If the number is 2
            // skip and continue
            if (i == 2)
                continue;
 
            System.out.print(i + " ");
        }
    }
}


Java
// Java program to demonstrates labeled continue statement
class GFG {
    public static void main(String args[])
    {
    // First label
    first:
        for (int i = 0; i < 3; i++) {
        // Second label
        second:
            for (int j = 0; j < 3; j++) {
                if (i == 1 && j == 1) {
 
                    // Using continue statement with label
                    continue first;
                }
                System.out.println(i + " " + j);
            }
        }
    }
}


输出
i: 0
i: 1
i: 2
i: 3
i: 4
Out of Loop

使用 break 作为 Goto 的一种形式

Java没有 goto 语句,因为它提供了一种以任意和非结构化方式进行分支的方法。 Java使用标签。标签用于标识代码块。

句法:

label:
{
  statement1;
  statement2;
  statement3;
  .
  .
}

现在,break 语句可以用来跳出目标块。我们不能打破任何没有为封闭块定义的标签。

句法:

break label;

例子:

Java

// Java program to demonstrates using break with goto
class GFG {
    public static void main(String args[])
    {
    // First label
    first:
        for (int i = 0; i < 3; i++) {
        // Second label
        second:
            for (int j = 0; j < 3; j++) {
                if (i == 1 && j == 1) {
 
                    // Using break statement with label
                    break first;
                }
                System.out.println(i + " " + j);
            }
        }
    }
}
输出
0 0
0 1
0 2
1 0

使用 break 来终止 switch 语句中的序列。

switch 语句是多路分支语句。它提供了一种简单的方法,可以根据表达式的值将执行分派到代码的不同部分。 break 语句用于在 switch 内部终止语句序列。 break 语句是可选的。如果省略,则执行将继续到下一个案例。

句法:

switch (expression)
{
  case value1:
    statement1;
    break;
  case value2:
    statement2;
    break;
  .
  .
  case valueN:
    statementN;
    break;
  default:
    statementDefault;
}

例子:

Java

// Java program to demonstrate using break to terminate a
// sequence in a switch statement.
class GFG {
    public static void main(String args[])
    {
        int i = 2;
        switch (i) {
        case 0:
            System.out.println("i is zero.");
            break;
        case 1:
            System.out.println("i is one.");
            break;
        case 2:
            System.out.println("i is two.");
            break;
        default:
            System.out.println("Invalid number");
        }
    }
}
输出
i is two.

继续:

Java中的 continue 语句用于跳过循环的当前迭代。我们可以在任何类型的循环中使用 continue 语句,例如 for、while 和 do-while 循环。基本上 continue 语句用于我们想要继续循环但不希望 continue 语句之后的剩余语句的情况。

句法:

continue;

使用 continue 继续循环

使用 continue ,我们可以跳过循环的当前迭代并立即跳转到循环的下一个迭代。

例子:

Java

// Java program to demonstrates the continue
// statement to continue a loop
class GFG {
    public static void main(String args[])
    {
        for (int i = 0; i < 10; i++) {
            // If the number is 2
            // skip and continue
            if (i == 2)
                continue;
 
            System.out.print(i + " ");
        }
    }
}
输出
0 1 3 4 5 6 7 8 9

使用 continue 作为带标签的 continue 语句

未标记的 continue 语句用于继续最里面的循环。但是,由于 JDK 1.5, Java引入了另一个称为标记 continue 语句的特性。我们可以使用带标签的 continue 语句来继续最外层的循环。

例子:

Java

// Java program to demonstrates labeled continue statement
class GFG {
    public static void main(String args[])
    {
    // First label
    first:
        for (int i = 0; i < 3; i++) {
        // Second label
        second:
            for (int j = 0; j < 3; j++) {
                if (i == 1 && j == 1) {
 
                    // Using continue statement with label
                    continue first;
                }
                System.out.println(i + " " + j);
            }
        }
    }
}
输出
0 0
0 1
0 2
1 0
2 0
2 1
2 2

中断和继续的区别:

Break

Continue

The break statement is used to terminate the loop immediately.

The continue statement is used to skip the current iteration of the loop.

break keyword is used to indicate break statements in java programming.

continue keyword is used to indicate continue statement in java programming.

We can use a break with the switch statement.

We can not use a continue with the switch statement.

The break statement terminates the whole loop early.

The continue statement brings the next iteration early.

It stops the execution of the loop.

It does not stop the execution of the loop.