While Loop
A while loop checks its condition before each iteration. It may run many times or not at all.
Use it when the number of repetitions depends on a changing condition rather than a fixed range known in advance.
Repeat while a condition holds
public class Main {
public static void main(String[] args) {
int remaining = 3;
while (remaining > 0) {
System.out.println("Remaining: " + remaining);
remaining--;
}
System.out.println("Complete");
}
}
Output:
Remaining: 3
Remaining: 2
Remaining: 1
Complete
The body prints the current count, then remaining-- subtracts 1 from it. -- is shorthand for remaining = remaining - 1, and its partner ++ adds 1. Once the value reaches zero, the condition fails and execution continues after the loop.
Every loop needs a path that can make its condition false. Forgetting the decrement here would create an infinite loop.
Complete the while condition and decrement to run the launch countdown.