Java if.. else if and else Statements Sxplained with 5 Examples

The if statement in Java

The if statement is a decision-making statement in Java that allows controlling the flow of program based on the given condition. As the condition given in the if statement is true, the program will execute the statements inside it.

If the condition is false, the execution will move outside of the if statement or if you have used else if and else statements, it will check the other conditions or execute the else part.

To understand that, consider you have a program that allows choosing the number of the day in a dropdown. If a user selects 1, the output will be displayed as “Monday”, for 2, it’s Tuesday and so on.

  • You may program this in Java with if and else if statements to display the output based on the user’s selection.
  • If the user is given only two choices then you may simply use the if..else statement. If options or outcomes are more than two then you may use multiple conditions with if and else if.

For learning more about if, if else and else statements, see the following section with demos and code. First, have a look at the general syntax of using the if statement.

Syntax of if Java statement

This is how you may use the Java if statement:

If (expression) {

 

// if condition is true, statements will execute

 

}

Note that, you may omit parenthesis in the if statement if only a single statement is used.

For more than one condition checks, you may use the else if statement as follows:

If (condition1) {

// if condition is true, statements will execute

}

else if (condiiton2){

// if condition2 is true, statements will execute

}

else if (condiiton3){

// if condition3 is true, statements will execute

}

else{

// if none of the condition is true, the else part will execute

}

An example of using simple if statement

In this example, only one condition is checked in an if statement. If the condition is true, a message will be displayed by using the System.out.println.

The condition is to check the value of a variable x. If the value is less than or equal to 10, the condition will be true and a message will display by executing a statement inside the if statement:

The Java code:

public class decision_making {

    public static void main(String []args) {

 

        int x;

        x=5;

        if (x <= 10) {

 

           System.out.println("The value of x is less than or equal to 10!");

 

        }

        }

}

Output:

Java if

As the value of the variable is 5, so condition is evaluated as true and the message is displayed. Now, what if the value of the variable was more than 10? See the output in the example below.

Java code with if statement:

public class decision_making {

    public static void main(String []args) {

 

        int x;

        x=15;

        if (x <= 10) {

 

           System.out.println("The value of x is less than or equal to 10!");

 

        }

        }

}

As you run this code, nothing will display as the condition became false. Logically, there should be some message in case the condition is false. For that, you may use the if statement with else as shown in the example below.

A demo of using Java else statement

I am using the same scenario as in above example i.e. only two outcomes; either the number is less than or to 10 OR its greater than that. In either case, a message will display by using the if else Java statement:

Java code:

public class decison_making {

    public static void main(String []args) {

 

        int x;

        x=15;

        if (x <= 10) {

           System.out.println("The value of x is less than or equal to 10!");

        }

        else {

                System.out.println("The condition is false; number is greater than 10!");

        }

        }

}

Output:

if else

As the value of variable x is 15, so condition became false and the print statement inside the else block executed and displayed the message.

A demo of else if statement

Now consider the same scenario i.e. evaluating the value of the variable and displaying a different message for the different range of values. For example,

  1. if the number is <= 10 then display “The variable value is less than or equal to 10”
  2. If number is <= 20 then display “The variable value is less than or equal to 20”
  3. If number is <= 30 then display “The variable value is less than or equal to 30”

And so on. Have a look at this example and its output with code:

Java code for else if:

public class decison_making {

    public static void main(String []args) {

 

        int x;

        x=35;

        if (x <= 10) {

           System.out.println("The value of x is less than or equal to 10!");

        }

        else if (x <= 20) {

           System.out.println("The value of x is less than or equal to 20!");

        }

        else if (x <= 30) {

            System.out.println("The value of x is less than or equal to 30!");

         }       

        else if (x <= 40) {

            System.out.println("The value of x is less than or equal to 40!");

         }        

        else if (x <= 50) {

            System.out.println("The value of x is less than or equal to 50!");

         }               

        else {

                System.out.println("The number is greater than 50!");

        }

        }

}

Result:

else if

You saw the value is set as 35, so the fourth condition is evaluated as true. If the value is set more than 50, the else part will execute.

Using the string expression in if…else if..else statements example

All above examples use the int type variable for demonstrating the if statement. In this example, a string type is used with if..else if and else statements. For that, a variable colorName is created and assigned a value initially.

Using the if statement, the color name is checked and a message is displayed accordingly. If none of the four colors is assigned to the variable, the else part will execute:

The code:

public class decison_making {

    public static void main(String []args) {

 

        String colorName;

 

        colorName="Red";

 

        if (colorName == "Green") {

 

           System.out.println("The value is Green!"); 

 

        }

 

        else if(colorName=="Red") {

 

           System.out.println("The value is Red");  

 

        }

 

        else if(colorName=="Orange") {

 

           System.out.println("The value is Orange");

 

        }

 

        else if(colorName=="Yellow") {

 

            System.out.println("The value is Yellow");

 

         }       

 

        else {

 

           System.out.println("A different color!");

 

        }

 

        }

}

Output:

else if string

The variable is assigned “Red”, so the second condition turned as true and a respective message is displayed as an output.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!