while Loop (Java)
A while loop repeats a block of code as long as a condition stays true. It is used when you don't know in advance how many times the loop should run.
Java
Make sure something inside the loop eventually makes the condition false, or you'll create an infinite loop. Use break to exit early and continue to skip to the next iteration.
Java also has a do-while loop, which checks the condition after running the body, so it always runs at least once.
Examples
Java
int count = 0;
while (count < 3) {
System.out.println(count);
count++;
}Output:
0
1
2