Home › Java Tutorial › Java while loop
Java while loop
Repeat while a condition is true.
while checks the condition first. do ... while runs the block once and then checks, so it always runs at least one time. Make sure something inside the loop changes the condition, or it never ends.
Example code
public class Main {
public static void main(String[] args) {
int i = 1;
while (i <= 3) {
System.out.println("i = " + i);
i++;
}
do {
System.out.println("once");
} while (false);
}
}