Java continue statement

Continue Statement in Java programming : Skip Part of Loop

Continue Statement in Java is used to skip the part of loop. Unlike break statement it does not terminate the loop , instead it skips the remaining part of the loop and control again goes to check the condition again.

Syntax : Continue Statement

{
//loop body 
-----------
-----------
-----------
-----------
continue;
-----------
-----------
}

Explanation :

  1. Continue Statement is Jumping Statement in Java Programming like break.
  2. Continue Statement skips the Loop and Re-Executes Loop with new condition.
  3. Continue Statement can be used only in Loop Control Statements such as For Loop | While Loop | do-While Loop.
  4. Continue is Keyword in Java Programming.

Different Ways of Writing Continue Statement in Java

Way 1 : Continue Statement Written Inside For Loop

continue statement in java Programming skipping loop

  1. Continue Statement is used to skip the remaining part of the for loop
for (int i = 0; i < max; i++) {
       // Interested only in p's
       if (searchMe.charAt(i) != 'p')
              continue;
      numPs++;  //Process P's
      }
  1. As shown in the above code snippet we can see , if we get any character other than p then we are using continue statement to skip the loop and if we get ‘P’ then we are incrementing Counter.
  2. Continue Statement in for loop takes control to “Increment” step. i.e after continue “i++” will gets executed. (show in diagram)

Way 2 : Continue Statement Written Inside While Loop

Continue Statement in While Loop
*Note : It will executes condition in while loop

Way 3 : Continue Statement Written Inside Do-While Loop

Continue Statement in do While Loop

Live Example : Continue Statement in Java Programming

In this example we are going to count the number of occurrences of “w”

class ContinueDemo {
    public static void main(String[] args) {
        String str = "www.c4learn.com";
        int max = str.length();
        int count = 0;
        for (int i = 0; i < max; i++) {
            if (str.charAt(i) != 'w')
                continue;
            count++;
        }
        System.out.println("Counting W : " + count );
    }
}

Output :

Counting W : 3
  • count statement is executed only 3 times as there are only 3 w’s present in string str.

Illegal Way of Writing Continue :

Way 1 : Continue as a Part of Normal Java Statement

class ContinueDemo {
    public static void main(String[] args) {
        String str = "www.c4learn.com";
        int max = str.length();
        int count = 0;
        continue;
        System.out.println("Hello continue");
    }
}

Above Statement will cause Compile time Error.

Way 2 : Don’t Use Continue Inside If

  1. Continue can be used inside If statement iff “If Statement” is part of any Loop.
  2. Don’t Use Continue inside if which is not a part of loop.
class ContinueDemo {
  public static void main(String[] args) {
     String str = "www.c4learn.com";
     int max = str.length();
     int count = 0;
     if(str.charAt(i) != 'p')
         continue;
     System.out.println("Hello continue");
    }
}