Previous Next

Java While Loop

The while loop is utilized to repeat a piece of code for as long as a certain condition remains true.

Syntax:

while (condition) {
  // block of code
}

Example:

public class Main {
  public static void main(String[] args) {
    int i = 1;
    while (i <= 8) {
      System.out.println(i);
      i++;
    }
  }
}

Output:

1
2
3
4
5
6
7
8
Previous Next