Tutorials

Java Tutorial

All Tutorials Learning Tools Login

Java Tutorial · Chapter 10 of 12

Java While Loop

Repeat while a condition is true.

while is useful for sentinel input and unknown repetition counts.

Update the loop variable inside the body.

While sample Try it Yourself »
public class Main {
    public static void main(String[] args) {
        int i = 1;
        while (i <= 5) {
            System.out.println(i);
            i++;
        }
    }
}

Classroom Tips

  • Avoid infinite loops by updating the condition.