Java continue statement Tutorial

In this section we will learn what the continue keyword is and how to use it in Java.

Note: we’re assuming you already familiar with either of these loops: `for`, `do while`, `while` or a statement like `switch`.

continue Statement in Java

The Java continue statement is used in loops to skip the current iteration of a loop and move on to the next iteration. Basically, using this statement it won’t end the execution of a loop entirely! It just skips the current one and jumps to the next iteration (if any) in the same loop.

Java continue statement syntax:

continue;

As you can see, this statement does not take an argument.

Example: for loop continue statement

public class Simple {
    public static void main(String[] args) {
        for (int i = 0; i<10; i++){
            if(i<5){
                continue;
            }
            System.out.print(i+", ");
        }
        System.out.println("\nDone!");
    }
}
Output: 
5, 6, 7, 8, 9, 
Done!

In this example, as long as the value of the `i` variable in the loop is less than the value 5, the iteration will be skipped by calling the `continue;` statement and the next iteration will start. That’s why those numbers that were less than 5 didn’t send to the output stream.

Example: while loop continue statement

import java.util.Arrays;
class Main{
	public static void main(String[] args) {
		int i =0;
        while (i<10){
        	i++;
            if(i<5){
                continue;
            }
            System.out.print(i+", ");
        }
        System.out.println("\nDone!");
    }
}
Output: 
5, 6, 7, 8, 9, 10,
Done!

Example: do-while continue statement

import java.util.Arrays;
class Main{
	public static void main(String[] args) {
		int i =0;
		do{
        	i++;
            if(i<5){
                continue;
            }
            System.out.print(i+", ");
        }
        while (i<10);
        System.out.println("\nDone!");
    }
}
Output: 
5, 6, 7, 8, 9, 10,
Done!

Java continue Statement in nested loop

We can use the `continue` statement within the body of a nested loop as well.

But remember, when using the `continue` statement within the body of a nested loop, this statement only skips the current iteration in the nested loop and not the entire loops!

Basically, the parent loop is still in its current loop, waiting for the inner loop to finish its work.

Example: nested loop and continue statement

import java.util.Arrays;
class Main{
	public static void main(String[] args) {
        for (int i = 0; i<3; i++){
            for (int b = 0; b<8; b++){
            	if(b<5){
                	continue;
            	}
            	System.out.println("The value of the b variable is: "+b);
            }
            System.out.println("The value of the i variable is: "+i);
        }
        System.out.println("Done!");
    }
}
Output: 
The value of the b variable is: 5
The value of the b variable is: 6
The value of the b variable is: 7
The value of the i variable is: 0
The value of the b variable is: 5
The value of the b variable is: 6
The value of the b variable is: 7
The value of the i variable is: 1
The value of the b variable is: 5
The value of the b variable is: 6
The value of the b variable is: 7
The value of the i variable is: 2
Done!

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies