Java if-else Statement



In this tutorial, we will learn about control flow statements using Java if and if...else statements with the help of examples.


What is if...else statement?

The if...else statement is a decision-making that is required when you want to execute a code only if a specific condition is fulfilled.

The if...else...if statement evaluates multiple expressions that generate TRUE or FALSE as a result. You need to determine which action to take and which statements to run if the result is TRUE or FALSE.


Java if (if-then) Statement

The if statement is used to specify if a block of Java code is to be executed not.

The syntax of an if-then statement can be given as follows:

if (condition) {
    // statements
}

Here above, the condition is a boolean expression such as count > 5.

  • if condition evaluates to true, statements are executed
  • if condition evaluates to false, statements are skipped

Working of if Statement

The following illustration shows the working of the Java if statement.

working of Java if statement

Example: Java if Statement

In the following example, we will see how to use the if statement.

public class Main {
    public static void main(String[] args) {
        
        int age = 22;
        
        // checks if age is more than 18    
        if (age < 18) {
            System.out.println("The age is under 18");
        }

        System.out.println("Statement outside of the if block");
    }
}

Output:

Statement outside of the if block

In the program above, the condition age < 18 is false. So, the code inside the parenthesis is skipped.


Example: Java if with String

In the following example, we will see how to use the if statement with string.

public class Main {
    public static void main(String[] args) {
        // create a string variable
        String greeting = "Hello";

        if (greeting == "Hello") {
            System.out.println("Hello World");
        }
    }
}

Output:

Hello World

In the above program, we are comparing two strings in the if block.


Java if...else Statement

The if statement runs a code section if the test condition is evaluated totrue. However, if the test condition is evaluated to false, it does nothing.

In this case, we can add an optional else block. Statements inside the else block are executed if the test condition is evaluated to false. This is called the if...else statement in Java.

The syntax of the if...else statement can be given as follows:

if (condition) {
    // codes inside the if block 
} 
else {
    // codes inside the else block
}

In the program above, we will execute statements inside the if block if the condition is true and the else block if the condition is false.


Working of if...else Statement

The following illustration shows the working of the Java if...else statement.

working of Java if-else statement

Example: Java if...else Statement

In the following example, we will see how to use the if...else statement.

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

        // checks if the age is greater than 18
        if(age > 18) {
            System.out.println("The age is greater than 18 years");
        }
        else {
            System.out.println("The age is less than 18 years");
        }

        System.out.println("Statement outside of the if...else block");
    }
}

Output:

The age is greater than 18 years
Statement outside of the if...else block

In the above program, we have a variable named age. Here, the test condition age > 18 checks if the age is greater than 18 years.

The passed value of the age is 22, the test condition evaluates to true. So the code inside the body of the if block is executed.

Let us change the value of the age to 14.

int age = 14;

After running the program with the new value of the age, the output will be:

The age is less than 18 years
Statement outside of the if...else block

Here above, the value of age is 14. The test condition evaluates to false. So the code inside the body of else is executed.


Java if...else...if Statement

Java offers an if...else...if mechanism that can be used to run one block of code among multiple other blocks.

if(condition_1) {
    // codes
}
else if(condition_2) {
    // codes
}
else if(condition_3) {
    // codes
}
.
.
.
else {
    // codes
}

Here, the if statements are executed from top to bottom. When the test condition is true, codes inside the body of that if block is executed. And, the program control jumps outside the if...else...if mechanism.

If all the test conditions are false, codes inside the body of else are executed.


Working of if...else...if Statement

The following illustration shows the working of the Java if...else...if statement.

working of Java if-else-if mechanism

Example: Java if...else...if Statement

In the following example, we will see how to use the if...else...if statement.

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

        if(age > 18) {
            System.out.println("The age is greater than 18");
        }
        else if(age < 18) {
            System.out.println("The age is under than 18");
        }
        else {
            System.out.println("The age is 18");            
        }
    }
}

Output:

The age is 18

In the program above, we are checking whether age is greater, under, or equal to 18.

Here we have two conditions:

  • age > 18 - checks if age is greater than 18
  • age < 18 - checks if age is under than 18

The value of age is 18. So both the conditions evaluate to false. So the statement inside the body of else is executed.


Java Nested if...else Statement

In Java, we can also use the if...else statement inside another if....else statement. It is called the nested if...else statement.

Let us see a program to find the largest of 3 numbers using the nested if...else statement.


Example: Nested if...else Statement

In the following example, we will see how to use the nested if...else statement.

public class Main {
    public static void main(String[] args) {
        Double a = 19.0, b = 37.0, c = 78.0, x;
        
        if(a >= b) {
            if(a >= c) {
                x = a;
            } else {
                x = c;
            }
        } else {
            if(b >= c){
                x = b;
            } else {
                x = c;
            }
        }

        System.out.println("The largest number is: " + x);
    }
}

Output:

The largest number is: 78.0


ExpectoCode is optimized for learning. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2020-2021 by ExpectoCode. All Rights Reserved.