Java if.. else if and else statements explained 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:

Java if

See online demo and code

The Java code:

 

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.

See online demo and code

The Java code with if statement:

 

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:

if else

See online demo and code

The Java code:

 

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:

else if

See online demo and code

The Java code for 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:

else if string

See online demo and code

The code:

 

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