If Statement

An if statement runs a block only when its condition is true. Code after that block continues either way.

Use this form when something should happen only under one condition and no alternative action is needed.

Check a positive number

public class Main {
    public static void main(String[] args) {
        int number = 8;

        if (number > 0) {
            System.out.println("Positive number");
        }
        System.out.println("Check complete");
    }
}

Output:

Positive number
Check complete

Since 8 is greater than zero, the message inside the if block is printed. Check complete is outside the block, so it is always printed.

The condition must be a boolean. Java does not treat numbers or strings as truthy values.

Check whether a number is divisible by 5.