Java for loop

A Java for loop combines initialization, a condition, and an update. The body runs while the condition is true.

A simple example

Print the numbers 1 to 3

  1. Start with Start at 1
  2. Do this Visit 1, 2, 3
  3. Result 1 → 2 → 3
Each pass prints one number on its own line. After 3 the loop has no more values to visit.
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        for (int n = 1; n <= 3; n++) {
            System.out.println(n);
        }
    }
}
Output
1
2
3
Bin 1: 3 items. Bin 2: Empty. Bin 3: 5 items.
Keep the printed bin numbers

Three supply bins contain 3, 0 and 5 items. Skip printing the empty middle bin but keep its position in the sequence. The last line must still refer to bin 3.