📜  Java中的Continue语句

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

Java中的Continue语句

假设一个人希望根据代码设计执行的值执行代码,但同一个用户强行想要跳过应该按照上述设计执行的代码的执行,但不会按照用户的要求执行.简单来说,它是根据用户的需求进行决策的问题。

现实生活中的例子:

在循环控制结构内的编程语言中经常使用 continue 语句。在循环内部,当遇到 continue 语句时,控件会直接跳转到下一次迭代的循环开头,而不是执行当前迭代的语句。当我们想要跳过特定条件并继续其余执行时使用 continue 语句。 Java continue 语句用于所有类型的 od 循环,但它通常用于 for、while 和 do-while 循环。

  • 在 for 循环的情况下, continue 关键字强制控制立即跳转到更新语句。
  • 而在 while 循环或 do-while 循环的情况下,控制立即跳转到布尔表达式。

语法: continue 关键字和分号

continue;

Continue语句流程图

上面的流程图对于理解这个关键字最重要。永远记住 条件总是放在菱形框内,语句放在矩形框中。现在跳到实现部分

案例 1:for 循环中的 continue 语句

在这个程序中,说明如何在 For 循环中使用 continue 语句。当 'i' 的值变为 10 或 12 时,continue 语句发挥作用并跳过它们的执行,但对于 'i' 的其他值,循环将顺利运行。

Java
// Java Program to illustrate the use of continue statement
 
// Importing Classes/Files
import java.util.*;
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // For loop for iteration
        for (int i = 0; i <= 15; i++) {
 
            // Check condition for continue
            if (i == 10 || i == 12) {
 
                // Using continue statement to skip the
                // execution of loop when i==10 or i==12
                continue;
            }
            // Printing elements to show continue statement
            System.out.print(i + " ");
        }
    }
}


Java
// Java Program to illustrate the use of continue statement
// inside the While loop
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Initializing a variable say it count  to a value
        // greater than the value greater among the loop
        // values
        int count = 20;
 
        // While loop for iteration
        while (count >= 0) {
            if (count == 7 || count == 15) {
                count--;
                // Decrementing variable initialized above
 
                // Showing continue execution inside loop
                // skipping when count==7 or count==15
                continue;
            }
 
            // Printing values after continue statement
            System.out.print(count + " ");
 
            // Decrementing the count variable
            count--;
        }
    }
}


Java
// Java Program to illustrate the use of continue statement
// inside the Do-While loop
 
// Importing generic Classes/Files
import java.util.*;
 
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating and Initializing a variable
        int i = 0;
 
        // Do-While loop for iteration
        do {
            if (i == 4 || i == 18) {
 
                // Incrementing loop variable by 2
                i += 2;
 
                // Illustrating continue statement skipping
                // the execution  when i==7 or i==15
                continue;
            }
 
            // Printing to showcase continue affect
            System.out.println(i);
 
            // Incrementing variable by 2
            i += 2;
 
            // Condition check
        } while (i <= 35);
    }
}


Java
// Java Program to illustrate the use of continue statement
// inside an inner loop or simply nested loops
 
// Importing generic Classes/Files
import java.util.*;
 
public class GFG {
 
    // Main drive method
    public static void main(String[] args)
    {
        // Outer loop for iteration
        for (int i = 1; i <= 4; i++) {
 
            // Inner loop for iteration
            for (int j = 1; j <= 3; j++) {
                if (i == 3 && j == 2) {
 
                    // Continue statement in inner loop to
                    // skip the execution when i==3 and j==2
 
                    continue;
                }
 
                // Print elements to showcase keyword affect
                System.out.println(i + " * " + j);
            }
        }
    }
}


输出 :

0 1 2 3 4 5 6 7 8 9 11 13 14 15 

情况 2:while 循环中的 continue 语句

在上面的程序中,我们举例说明了如何在 While 循环中使用 continue 语句。当 count 的值变为 7 或 15 时,continue 语句会发挥作用并跳过它们的执行,但对于 count 的其他值,循环将顺利运行。

Java

// Java Program to illustrate the use of continue statement
// inside the While loop
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Initializing a variable say it count  to a value
        // greater than the value greater among the loop
        // values
        int count = 20;
 
        // While loop for iteration
        while (count >= 0) {
            if (count == 7 || count == 15) {
                count--;
                // Decrementing variable initialized above
 
                // Showing continue execution inside loop
                // skipping when count==7 or count==15
                continue;
            }
 
            // Printing values after continue statement
            System.out.print(count + " ");
 
            // Decrementing the count variable
            count--;
        }
    }
}

输出:

20 19 18 17 16 14 13 12 11 10 9 8 6 5 4 3 2 1 0 

案例 3:do while 循环中的 continue 语句

在上面的程序中,我们举例说明了如何在 do-While 循环中使用 continue 语句。当 i 的值变为 4 或 18 时,continue 语句发挥作用并跳过它们的执行,但对于 i 的其他值,循环将顺利运行。

Java

// Java Program to illustrate the use of continue statement
// inside the Do-While loop
 
// Importing generic Classes/Files
import java.util.*;
 
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating and Initializing a variable
        int i = 0;
 
        // Do-While loop for iteration
        do {
            if (i == 4 || i == 18) {
 
                // Incrementing loop variable by 2
                i += 2;
 
                // Illustrating continue statement skipping
                // the execution  when i==7 or i==15
                continue;
            }
 
            // Printing to showcase continue affect
            System.out.println(i);
 
            // Incrementing variable by 2
            i += 2;
 
            // Condition check
        } while (i <= 35);
    }
}

输出:

0
2
6
8
10
12
14
16
20
22
24
26
28
30
32
34

情况 4:内循环(嵌套循环)中的 continue 语句

在上面的程序中,我们举例说明了如何在嵌套循环中使用 continue 语句。当 i 的值变为 3 且 j 的值变为 2 时,continue 语句发挥作用并跳过它们的执行,但对于 i 和 j 的其他值,循环将顺利运行。

Java

// Java Program to illustrate the use of continue statement
// inside an inner loop or simply nested loops
 
// Importing generic Classes/Files
import java.util.*;
 
public class GFG {
 
    // Main drive method
    public static void main(String[] args)
    {
        // Outer loop for iteration
        for (int i = 1; i <= 4; i++) {
 
            // Inner loop for iteration
            for (int j = 1; j <= 3; j++) {
                if (i == 3 && j == 2) {
 
                    // Continue statement in inner loop to
                    // skip the execution when i==3 and j==2
 
                    continue;
                }
 
                // Print elements to showcase keyword affect
                System.out.println(i + " * " + j);
            }
        }
    }
}

输出:

1 * 1
1 * 2
1 * 3
2 * 1
2 * 2
2 * 3
3 * 1
3 * 3
4 * 1
4 * 2
4 * 3