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) {
    /* code to be execute if the condition_expression is true */
    statements;
} else {
    /* code to be execute if the condition_expression is false */
    statements;
}

Java If Else statement control flow diagram

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) {
    statement1;
} else if (condition_expression_Two) {
    statement2;
} else if (condition_expression_Three) {
    statement3;
} else {
    statement4;
}

It test the conditions in sequence, from top to bottom. An else-if condition is tested only if all previous conditions in if-else 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.

Java If Else Ladder Statement Control Flow Diagram