Java Continue Statement, Label loop

Java Continue Statement, Continue Statement in Java, Continue Examples in java, Java continue in inner loop, inner loop continue example, Continue Statement Examples.

Continue statement is used to skip the current loop iteration when a condition is satisfied.
Mainly used in while or for loop.

Syntax:

continue;

Example Flow chart:

Java Continue Statement, Label loop




Example Program:

package com.adeepdrive.switchdemo;
public class ContinueExample {
    public static void main(String[] args) {
        System.out.println("Printing even numbers");
        for (int i = 0; i <= 10; i++) {
            if (i % 2 == 1) {
                continue;
            }
            System.out.println(i);
        }
    }
}
Output: Printing even numbers 0 2 4 6 8 10 Some of you may expect the loop to print from 0 to 10 but there we have condition if the remainder of value i is equal to 1 then continue statement will be executed. First it will skip the lines after the continue and go for next iteration value. Here it is skipped 5 times.

Inner loop continue example:

 

It works in inner loop only skips the current inner loop iteration and not skip outer loop iteration.
package com.adeepdrive.switchdemo;
public class ContinueExample {
    public static void main(String[] args) {
        System.out.println("Printing even numbers");
        for (int i = 0; i <= 10; i++) {
            for (int j = 0; j < 10; j++) {
                if (j % 2 == 1) {
                    continue;
                }
                System.out.println("i value:: " + i + " j value :: " + j);
            }
        }
    }
}
Output:


Printing even numbers
i value:: 0 j value :: 0
i value:: 0 j value :: 2
i value:: 0 j value :: 4
i value:: 0 j value :: 6
i value:: 0 j value :: 8
i value:: 1 j value :: 0
i value:: 1 j value :: 2
i value:: 1 j value :: 4
i value:: 1 j value :: 6
i value:: 1 j value :: 8
i value:: 2 j value :: 0
i value:: 2 j value :: 2
i value:: 2 j value :: 4
i value:: 2 j value :: 6
i value:: 2 j value :: 8
i value:: 3 j value :: 0
i value:: 3 j value :: 2
i value:: 3 j value :: 4
i value:: 3 j value :: 6
i value:: 3 j value :: 8
i value:: 4 j value :: 0
i value:: 4 j value :: 2
i value:: 4 j value :: 4
i value:: 4 j value :: 6
i value:: 4 j value :: 8
i value:: 5 j value :: 0
i value:: 5 j value :: 2
i value:: 5 j value :: 4
i value:: 5 j value :: 6
i value:: 5 j value :: 8
i value:: 6 j value :: 0
i value:: 6 j value :: 2
i value:: 6 j value :: 4
i value:: 6 j value :: 6
i value:: 6 j value :: 8
i value:: 7 j value :: 0
i value:: 7 j value :: 2
i value:: 7 j value :: 4
i value:: 7 j value :: 6
i value:: 7 j value :: 8
i value:: 8 j value :: 0
i value:: 8 j value :: 2
i value:: 8 j value :: 4
i value:: 8 j value :: 6
i value:: 8 j value :: 8
i value:: 9 j value :: 0
i value:: 9 j value :: 2
i value:: 9 j value :: 4
i value:: 9 j value :: 6
i value:: 9 j value :: 8
i value:: 10 j value :: 0
i value:: 10 j value :: 2
i value:: 10 j value :: 4
i value:: 10 j value :: 6
i value:: 10 j value :: 8

Label Continue Statement example:

 

If we want to move to outer loop when continue encounters in inner loop. We can achieve this using label concept.

package com.adeepdrive.switchdemo;
public class ContinueExample {
    public static void main(String[] args) {
        System.out.println("Printing even numbers");
       
        Outer:
        for (int i = 0; i <= 10; i++) {
            for (int j = 0; j < 10; j++) {
                if (j % 2 == 1) {
                    continue Outer;
                }
                System.out.println("i value:: " + i + " j value :: " + j);
            }
        }
    }
}
 
Output: 

Printing even numbers
i value:: 0 j value :: 0
i value:: 1 j value :: 0
i value:: 2 j value :: 0
i value:: 3 j value :: 0
i value:: 4 j value :: 0
i value:: 5 j value :: 0
i value:: 6 j value :: 0
i value:: 7 j value :: 0
i value:: 8 j value :: 0
i value:: 9 j value :: 0
i value:: 10 j value :: 0

0 Comments