java
  1. java-continue

Java Continue Statement

Syntax

continue;

Example

for (int i = 0; i < 10; i++) {
  if (i == 4) {
    continue;
  }
  System.out.println(i);
}

Output

0
1
2
3
5
6
7
8
9

Explanation

In Java, the continue statement is used inside loops to skip the current iteration of the loop and move on to the next iteration. When the continue keyword is encountered, the remaining statements in the current iteration are skipped and the loop proceeds to the next iteration.

In the example above, the for loop iterates from 0 to 9. When the value of i is equal to 4, the continue statement is executed, skipping the remaining statements in the current iteration and proceeding to the next iteration. As a result, the value of 4 is not printed.

Use

The continue statement can be used in loops to skip over certain iterations that do not meet a particular condition. This can be useful for skipping over certain array elements, or for ignoring specific values in a loop.

Important Points

  • The continue statement is used inside loops to skip the current iteration and proceed to the next iteration.
  • When the continue statement is encountered, the remaining statements in the current iteration are skipped.
  • The continue statement can be useful for skipping over certain array elements or for ignoring specific values in a loop.
  • Be careful when using continue, as it can lead to an infinite loop if not used properly.

Summary

The continue statement is an essential construct in Java that allows for skipping over certain iterations in a loop. It can be used to ignore specific values or elements in a loop, and can help make loop constructs more efficient. It is important to use continue properly, as it can lead to unwanted behavior if not used correctly.

Published on: