Java continue statement - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Sunday, November 4, 2018

Java continue statement

Continue statement in Java

The continue statement is used to control the flow of execution of loops. In while and do-while loops, a continue statement causes control to be transferred directly to the conditional expression that controls the loop. In a for loop, control goes first to the iteration portion of the for statement and then to the conditional expression. For all three loops, any intermediate code is bypassed.

Example:

// continue statement with for loop
public class continueForLoop {
  public static void main(String[] args) {

    for (int i = 0; i < 10; i++) {
      System.out.print(i + " ");

      if (i % 2 == 0) continue;

      System.out.println();
    }
  }
}

Output:

0 1
2 3
4 5
6 7
8 9

continue statement with label for loop

As you can use label break statement, continue can also use the label to describe which enclosing loop to continue.

Example:

Print triangular multiplication table for 0 to 9 using the continue statement.

/ Using continue with a label.
class TriangularMultiplication {
  public static void main(String args[]) {

    outer:
    for (int i = 0; i < 10; i++) {

      for (int j = 0; j < 10; j++) {

        if (j > i) {
          System.out.println();
          continue outer;
        }

        System.out.print(i * j + " ");
      } // inner loop
    } // outer loop
    System.out.println("\nOutside the outer label for loop");
  }
}

Output:

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

Java continue statement with do-while loop

Example:

// continue statement with do-while loop
public class ContinueDoWhileLoop {
  public static void main(String[] args) {
    int i = 0;
    do {
      if (i == 5) continue;

      System.out.println("Value of i= " + i);

    } while (++i < 10);

    System.out.println("Outside the do-while loop.");
  }
}

Output:

Value of i= 0
Value of i= 1
Value of i= 2
Value of i= 3
Value of i= 4
Value of i= 6
Value of i= 7
Value of i= 8
Value of i= 9
Outside the do-while loop.

As you can see in the above output. Value of i= 5 is not printed.

Java continue statement with while loop

Example:

/ continue statement with while loop
public class ContinueWhileLoop {
  public static void main(String[] args) {
    int i = 0;
    while (i < 5) {
      i++; // value of i is increased by 1
      if (i == 2) continue;
      System.out.println("Value of i= " + i);
    }

    System.out.println("Outside the while loop.");
  }
}

Output:

Value of i= 1
Value of i= 3
Value of i= 4
Value of i= 5
Outside the while loop.

Java break statement inside nested loop

Java continue statement will continue the inner loop only when if you used the continue statement inside it.

Example:

// demonstrate continue statement with nested loop
public class ContinueNestedtLoop {

  public static void main(String[] args) {
    // Outer loop
    for (int i = 0; i < 3; i++) {
      System.out.println("Outer loop: i= " + i);

      // Inner loop
      for (int j = 1; j <= 3; j++) {
        if (j == 2) continue; // continue the inner loop

        System.out.println(" Inner loop: j= " + j);
      }
    }
  }
}

Output:

Outer loop: i= 0
 Inner loop: j= 1
 Inner loop: j= 3
Outer loop: i= 1
 Inner loop: j= 1
 Inner loop: j= 3
Outer loop: i= 2
 Inner loop: j= 1
 Inner loop: j= 3

1 comment :

  1. factorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.
    factorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.
    factorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.


    ReplyDelete