If Else

Add else when exactly one of two paths should run. Java checks the if condition first. If it is false, the else block runs.

Pass or fail

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

        if (marks > 40) {
            System.out.println("Pass");
        } else {
            System.out.println("Fail");
        }
    }
}

Output:

Pass

Since 75 is greater than 40, the if condition is true and the program prints Pass. If marks were 40 or less, the program would print Fail instead.

Braces are optional around a one-statement branch, but keeping them prevents subtle bugs when another statement is added later.

Use if and else to print a voting eligibility message.