Utilizor
Contact Us

Java While Loop

Loops through a block of code while a specified condition is true.

Examples

Basic While Loop

Counts from 0 to 4.

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

Do/While Loop

Executes the block once, then checks condition.

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

Test Your Knowledge

Java Quiz

No quiz available for this topic yet.