Skip to content
Veltoryx Technologies
ProLesson 2.4Beginner3 min readUpdated 30 October 2026

if, else and switch

Make decisions with if, else if and else, then use switch the classic way and the modern way with arrows and switch expressions.

Programs need to make decisions: give a discount if the cart is big, show an error if the password is wrong. Java has two tools for this: if and switch.

01if, else if, else

Grades.java
public class Grades {
    public static void main(String[] args) {
        int marks = 78;
        if (marks >= 90) {
            System.out.println("Grade A");
        } else if (marks >= 75) {
            System.out.println("Grade B");
        } else if (marks >= 40) {
            System.out.println("Grade C");
        } else {
            System.out.println("Fail");
        }
    }
}
Expected output
Grade B

Java checks the conditions top to bottom and runs only the first one that's true. Order matters: if marks >= 40 came first, everyone above 40 would get a C.

Loading the rest of the lesson…