Tutorial

Java continue statement

Published on August 4, 2022
author

Pankaj

Java continue statement

Java continue statement is used to skip the current iteration of a loop. Continue statement in java can be used with for, while and do-while loop.

Java continue statement

When continue statement is used in a nested loop, it only skips the current execution of the inner loop. Java continue statement can be used with label to skip the current iteration of the outer loop too. Let’s have a look at some continue java statement examples.

Java continue for loop

Let’s say we have an array of integers and we want to process only even numbers, here we can use continue loop to skip the processing of odd numbers.

package com.journaldev.java;

public class JavaContinueForLoop {

	public static void main(String[] args) {
		int[] intArray = { 1, 2, 3, 4, 5, 6, 7 };

		// we want to process only even entries
		for (int i : intArray) {
			if (i % 2 != 0)
				continue;
			System.out.println("Processing entry " + i);
		}
	}

}

java continue statement, java continue for loop

Java continue while loop

Let’s say we have an array and we want to process only index numbers divided by 3. We can use java continue statement here with while loop.

package com.journaldev.java;

public class JavaContinueWhileLoop {

	public static void main(String[] args) {
		int[] intArray = { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
		int i = 0;
		while (i < 10) {

			if (i % 3 != 0) {
				i++;
				continue;
			}
			System.out.println("Processing Entry " + intArray[i]);
			i++;
		}
	}

}

java while loop continue

Java continue do-while loop

We can easily replace above while loop code with do-while loop as below. Result and effect of continue statement will be same as above image.

do {

	if (i % 3 != 0) {
		i++;
		continue;
	}
	System.out.println("Processing Entry " + intArray[i]);
	i++;
} while (i < 10);

Java continue label

Let’s have a look at java continue label example to skip the outer loop processing. We will use two dimensional array in this example and process an element only if all the elements are positive numbers.

package com.journaldev.java;

import java.util.Arrays;

public class JavaContinueLabel {

	public static void main(String[] args) {

		int[][] intArr = { { 1, -2, 3 }, { 0, 3 }, { 1, 2, 5 }, { 9, 2, 5 } };

		process: for (int i = 0; i < intArr.length; i++) {
			boolean allPositive = true;
			for (int j = 0; j < intArr[i].length; j++) {
				if (intArr[i][j] < 0) {
					allPositive = false;
					continue process;
				}
			}
			if (allPositive) {
				// process the array
				System.out.println("Processing the array of all positive ints. " + Arrays.toString(intArr[i]));
			}
			allPositive = true;
		}

	}

}

java continue label

Java continue important points

Some important points about java continue statement are;

  1. For simple cases, continue statement can be easily replaced with if-else conditions but when we have multiple if-else conditions then using continue statement makes our code more readable.
  2. continue statement comes handy incase of nested loops and to skip a particular record from processing.

I have made a short video explaining java continue statement in detail, you should watch it below. https://www.youtube.com/watch?v=udqWkqhc2kw Reference: Oracle Documentation

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
May 26, 2019

This whole code: if (allPositive) { // process the array System.out.println("Processing the array of all positive ints. " + Arrays.toString(intArr[i])); } allPositive = true; is unnecessary. I had to double check in my own IDE, to make sure. I usually like your articles, so this one felt like a letdown. You better fix it up. No need for a conditional in the outer loop, because that conditional would only get reached if the array is positive.

- Noone

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    October 16, 2017

    It is a “go to” statement, which was recommended to avoid in your code. I have not seen these statements in professional code in years?? Not sure we should bring them back??

    - Janice

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      July 20, 2017

      great article nice very very nice thanks

      - Anurag Singh

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        April 3, 2015

        Hi Pankaj, Nice Tutorials. I think in the above example for processing all positive elements, we are assigning the allPositive value to true at line 33. I think its unnecessary. As we are assigning its value to true in the beginning of each iteration. Thanks.

        - Raghuveer Kurdi

          Try DigitalOcean for free

          Click below to sign up and get $200 of credit to try our products over 60 days!

          Sign up

          Join the Tech Talk
          Success! Thank you! Please check your email for further details.

          Please complete your information!

          Become a contributor for community

          Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

          DigitalOcean Documentation

          Full documentation for every DigitalOcean product.

          Resources for startups and SMBs

          The Wave has everything you need to know about building a business, from raising funding to marketing your product.

          Get our newsletter

          Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

          New accounts only. By submitting your email you agree to our Privacy Policy

          The developer cloud

          Scale up as you grow — whether you're running one virtual machine or ten thousand.

          Get started for free

          Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

          *This promotional offer applies to new accounts only.