If Else Statement in Java

If..else statement in java first evaluates conditional expression. If conditional expression evaluates to true then if block gets executed otherwise else block gets executed.

Unlike if statement, if..else statement provides a alternate path of execution whenever conditional expression evaluates to false. Only either if block or else block can get executed at a time not both.

If else statement is used when we want to execute different set of statements depending upon whether certain condition is true of false.

Syntax of if..else statement

if(condition_expression) {
    statements;
} else {
    statements;
}
Java If Else statement control flow diagram

Java if else statement program

public class IfElseStatement {
    public static void main(String[] args) {
        int num = 10;

        if ((num % 2) == 0) {
            System.out.println(num + " is Even");
        } else {
            System.out.println(num + " is Odd");
        }
    }
}
Output
10 is Even

If..Else Ladder Statement in Java

If..else ladder statement in java is used when we want to test multiple conditions and execute different code blocks for different conditions. After the if block we can add multiple if..else block to check multiple conditions.

Syntax of if-else ladder statement

if(condition_expression_One) {
    statements;
} else if (condition_expression_Two) {
    statements;
} else if (condition_expression_Three) {
    statements;
} else {
    statements;
}

It evaluates the conditions in sequence, from top to bottom. An else-if condition is evaluated only if all previous conditions in ladder evaluated false. If any of the conditional expression evaluates to true, then it will execute the corresponding code block and exits whole if-else ladder statement.

Java If Else Ladder Statement Control Flow Diagram

Java if else ladder statement program

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

        if (marks < 40) {
            System.out.println("FAILED");
        } else if (marks >= 40 && marks < 60) {
            System.out.println("GRADE C");
        } else if (marks >= 60 && marks < 80) {
            System.out.println("GRADE B");
        } else {
            System.out.println("GRADE A");
        }
    }
}
Output
GRADE A