Start Coding Now

If-Else Statements in Java - Conditional Logic

Learn how to use if, else-if, and else statements in Java to control program flow based on conditions.

If Statement

Executes a block of code if the condition is true.

if (condition) {
    // code
}

If-Else Statement

Executes one block if true, another if false.

int time = 20;
if (time < 18) {
    System.out.println("Good day.");
} else {
    System.out.println("Good evening."); // Prints this
}

Else-If Ladder

Checks multiple conditions.

int score = 85;

if (score >= 90) { System.out.println("A Grade"); } else if (score >= 80) { System.out.println("B Grade"); } else { System.out.println("C Grade"); }

Frequently Asked Questions