Continue_keyWord

╰半夏微凉° 2021-11-05 06:44 241阅读 0赞

Java continue Statement with Examples

In this quick article, we will discuss how and when to use the continue statement with examples.
The continue statement skips the current iteration of a for, while or do-while loop. The unlabeled form skips to the end of the innermost loop’s body and evaluates the boolean expression that controls the loop.

Table of contents

Flow Diagram of a continue Statement
continue Statement Inside for Loop Example

  • Using continue Statement in while Loop Example
  • Using continue Statement in do-while Loop Example
  • Using continue Statement with Labeled for Loop
    Syntax
    continue word followed by a semicolon.
    continue;

1. Flow Diagram of a continue Statement

2. continue Statement Inside for Loop Example

  1. package net.javaguides.corejava.controlstatements.loops;
  2. public class ContinueExample {
  3. public static void main(String args[]) {
  4. for (int i = 0; i < 10; i++) {
  5. System.out.print(i + " ");
  6. if (i % 2 == 0)
  7. continue;
  8. System.out.println("");
  9. }
  10. }
  11. }

This code uses the % operator to check if i is even. If it is, the loop continues without printing a newline. Here is the output from this program:
0 1
2 3
4 5
6 7
8 9

3. Using continue Statement in while Loop Example

  1. package net.javaguides.corejava.controlstatements.loops;
  2. public class ContinueExample {
  3. public static void main(String args[]) {
  4. int count = 10;
  5. while (count >= 0) {
  6. if (count == 7) {
  7. count--;
  8. continue;
  9. }
  10. System.out.println(count + " ");
  11. count--;
  12. }
  13. }
  14. }

Output:

  1. 10
  2. 9
  3. 8
  4. 6
  5. 5
  6. 4
  7. 3
  8. 2
  9. 1
  10. 0

Note that we are iterating this loop from 10 to 0 for counter value and when the counter value is 7 the loop skipped the print statement and started the next iteration of the while loop.

4. Using continue Statement in do-while Loop Example

  1. package net.javaguides.corejava.controlstatements.loops;
  2. public class ContinueExample {
  3. public static void main(String args[]) {
  4. int j = 0;
  5. do {
  6. if (j == 7) {
  7. j++;
  8. continue;
  9. }
  10. System.out.print(j + " ");
  11. j++;
  12. } while (j < 10);
  13. }
  14. }

Output:

  1. 0 1 2 3 4 5 6 8 9

Note that we have skipped iteration when j==7.

5. Using continue Statement with Labeled for Loop

As with the break statement, continue may specify a label to describe which enclosing loop to continue. Here is an example program that uses continue to print a triangular multiplication table for 0 through 9:

  1. package net.javaguides.corejava.controlstatements.loops;
  2. public class ContinueLabel {
  3. public static void main(String args[]) {
  4. outer: for (int i = 0; i < 10; i++) {
  5. for (int j = 0; j < 10; j++) {
  6. if (j > i) {
  7. System.out.println();
  8. continue outer;
  9. }
  10. System.out.print(" " + (i * j));
  11. }
  12. }
  13. System.out.println();
  14. }
  15. }

Output:

  1. 0
  2. 0 1
  3. 0 2 4
  4. 0 3 6 9
  5. 0 4 8 12 16
  6. 0 5 10 15 20 25
  7. 0 6 12 18 24 30 36
  8. 0 7 14 21 28 35 42 49
  9. 0 8 16 24 32 40 48 56 64
  10. 0 9 18 27 36 45 54 63 72 81

发表评论

表情:
评论列表 (有 0 条评论,241人围观)

还没有评论,来说两句吧...

相关阅读