print
Advertisment
Advertisment

Continue statement in java

Java continue statement is used to continue the loop. It continues the current flow of the program if you entered a condition in the loop. Then the continue statement skips that condition and continues the further flow of the program until that condition is met. We can use Java continue statement in all types of loops such as for loop, while loop, do-while loop, and for-each.

Syntax

continue;

java continue statement with use for loop

class Continue{
  public static void main(String[] args) {
    for(int i=1;i<=8;i++){
      if(i==4){   
        continue;   //it will skip 4
      }
      System.out.println(i);
    }
  }
}

Output :


  1
  2
  3
  5
  6
  7
  8

java continue statement with use for-each loop

class For_each {  
  public static void main(String[] args) {  
    int num[] = new int[]{10,20,30,40,50,60,70};
    for (int s:num ) {
      if(s==40){
        continue;
      }
      System.out.println(s);
    }
  }
}

Output :


  10
  20
  30
  50
  60
  70
Advertisment

java continue statement with use do-while statement

class Continue1 {  
  public static void main(String[] args) {  
    int i=1;  
    do{  
      if(i==5){  
        i++;
        continue; 
      }  
      System.out.println(i);  
      i++;  
    }while(i<=10);  
  }
}

Output :


   1
   2
   3
   4
   6
   7
   8
   9           
   10
Advertisment

Difference Between Break and Continue Statement in Java

Break Statement

  • The break statement begins with the break keyword.
  • The break statement is used to terminate the flow of the program.
  • The break statement is also known as the jump statement.
  • Syntax of break statement
    break;

Continue Statement

  • The continue statement begins with the continue keyword.
  • The continue statement is used to skip the value of the condition.
  • Continue statement is also known as jump statement.
  • Syntax of continue statement
    continue;
Advertisment
Advertisment
arrow_upward