continue keyword in Java

continue keyword is used to stop the execution of current iteration and start the execution of next iteration in loops such as for loop , while loop.

The statements after the continue keyword won’t be executed.

Example with for loop

  1. public class MainProgram
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         for (int i = 1; i <= 100; i++)
  6.         {
  7.             if(i % 10 != 0)
  8.             {
  9.                 continue;
  10.             }
  11.              
  12.             System.out.println(i);
  13.         }
  14.     }
  15. }
public class MainProgram
{
    public static void main(String[] args) 
    {
        for (int i = 1; i <= 100; i++)
        {
            if(i % 10 != 0)
            {
                continue;
            }
             
            System.out.println(i);
        }
    }
}


In the above program,we are printing only those numbers which are divisible by 10.

If number is not divisible by 10 then continue statement is executed and it will stop further execution of code and move to the next iteration in loop.

Similarly, continue can be used in while loop

Example with while loop


The following while loop will produce a list of even numbers from 1 to 100:

  1. int count = 1;
  2.  
  3. while (count <= 100) {
  4.  
  5.     if (count % 2 != 0) {
  6.  
  7.         count++;
  8.  
  9.         continue;
  10.     }
  11.  
  12.     System.out.println(count);
  13.  
  14.     count++;
  15. }
int count = 1;

while (count <= 100) {

    if (count % 2 != 0) {

        count++;

        continue;
    }

    System.out.println(count);

    count++;
}


In the above program,we are printing only even numbers.

If number is not divisible by 2 then continue statement is executed and it will stop further execution of code and move to the next iteration in loop.

Note:
The continue keyword is used to skip the further execution of statements and start the next iteration of a for or while loop.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on