Java do-while loop

A Java do-while loop runs its body before testing the condition, so it always executes at least once.

A simple example

Print first, then test

  1. Start with n starts at 1
  2. Do this Print n, add 1
  3. Result Stop at 4
The first print happens before the stopping test. The loop prints 1, 2 and 3 on separate lines.
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        int n = 1;
        do {
            System.out.println(n);
            n++;
        } while (n <= 3);
    }
}
Output
1
2
3
First check: Busy. Second check: Ready. Checks made: 2.
Check a printer until it is ready

A print desk checks the printer once before deciding whether to check again. If the printer answers ready on the second check, the loop stops there. A limit of three checks keeps an offline printer from holding up the desk.