Java continue statement

In this chapter you will learn:

  1. What is Java continue statement
  2. Syntax for Java continue statement
  3. Example - Java continue statement
  4. How to use continue and label together

Description

continue statement forces an early iteration of a loop. In while and do-while loops, a continue statement causes control to be transferred to the conditional expression that controls the loop. In a for loop, control goes first to the iteration portion of the for statement and then to the conditional expression.

Syntax

continue;

or

continue labelName;

Example

The following code shows how to use a continue statement.

 
public class Main {
  public static void main(String[] argv) {
    for (int i = 0; i < 10; i++) {
      System.out.print(i + " ");
      if (i % 2 == 0)
        continue;
      System.out.println("");
    }/* w  w  w .ja va 2  s  .  co  m*/
  }
}

The code above generates the following result.

Example 2

continue may specify a label to describe which enclosing loop to continue.

 
public class Main {
  public static void main(String args[]) {
    outer: for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        if (j > i) {
          System.out.println();/*w  w w  .ja va2 s.c o  m*/
          continue outer;
        }
        System.out.print(" " + (i * j));
      }
    }
    System.out.println();
  }
}

Here is the output of this program:

Next chapter...

What you will learn in the next chapter:

  1. What are the three types of comment in Java
  2. How to use Java single line comment
  3. How to use Java multiline statement
Home »
  Java Tutorial »
    Java Langauge »
      Java Statement
Java if Statement
Java if else Statement
Java if else ladder statement
Java nested if statement
Java switch Statement
Java for loop
Java for each loop
Java while Loop
Java do while loop
Java break statement
Java continue statement
Java Comments
Java documentation comment(Javadoc)