Java if, else if, and else

Java uses if, else if, and else to select a branch. Each condition must have a boolean value.

A simple example

Follow one branch

  1. Start with n is 2
  2. Do this Is n positive?
  3. Result yes
The condition selects the first branch. Changing the value to zero selects the other branch.
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        int n = 2;
        if (n > 0) {
            System.out.println("yes");
        } else {
            System.out.println("no");
        }
    }
}
Output
yes
On the shelf: 3 books. Low-stock limit: 5 books. Shelf notice: Order soon.
Decide when to restock

A student shop has three sketchbooks left. Empty stock gets a sold-out notice. A positive count of five or fewer gets a reorder notice, so three tells the shopkeeper to order soon.