Java if – The Decision Making Statements in Java

The Java ‘If’ comes wherever options come. Where there are more than one options, we have to decide. In the computer programming, we use one of the methods to handle this is by using the If and if else statements, also known as the decision-making statements – to take the decisions based on different situations.

In the computer programming, we use one of the methods to handle this is by using the If and if else statements, also known as the decision-making statements – to take the decisions based on different situations.

See if example online
An if else example

Types of decision-making statement in Java

Two types of decision-making statements provided by Java are:

  1. The If and if else statements
  2. The Switch statement

This chapter will explain the if statement of Java only. For the switch statement go to the Java switch chapter.

How to use the Java if statement?

The general Syntax of the if statement in Java:

If (condition) {

// Code to be executed if condition is true

}

The if condition is evaluated by the Boolean variables.

As you can see, the if keyword is followed by a condition. If the condition evaluates as true the statement(s) in curly braces will execute. If the condition is false, the statement(s) after the closing curly braces will execute.

Example of using the if statement

The example below uses two variables, a and b. The if condition checks whether the variable a value is less than b. If the variable a’s value is less than b then it will execute statement inside the curly braces. See the example by clicking the image or link below:

Experience this online

You can see, a is given the value = 10 while b = 20. The if statement checks if the value of a is less than b? As this is true, the statement inside the curly braces will execute and print this message on the screen:

This is if statement example

The if else statement

In the above section, we only used a simple if statement. If a condition turns out to be true then it will execute statements in the curly braces. What if that condition was false?

The execution will go out of the if block to the next line of code. However, you may want to perform some action or show some message if the condition is false. The Java if else statement can be used with the if statement when a condition is false.

The Syntax of using if else:

If (condition) {

// Code to be executed if condition is true

}else {

// Code to be executed if condition is true

}

Example of using if else Java statement

The following example uses two integer variables, a and b. The if condition checks whether the variable a value is less than b. If variable a value is less than b then it will execute the statement inside the curly braces. Otherwise, the block of code inside the Java else statement will be executed.

Experience this online

As you can see, the output of the above program will be:

The else block executed

Multiple if else statements in Java

In the above example, we only used Java’s if statement with the else. What if you have more than two options to make a decision?

For example, if the color is green then execute these statements, if red then these, if orange then these and if any other then else block should be executed. For that, you can use else if statements, to check more than two options in the if block.

The Syntax of using the else if in Java:

If (condition) {

// Code to be executed if condition is true

else if{

// Code to be executed if condition is true

}

else if{

// Code to be executed if condition is true

}

}else {

// Code to be executed if condition is true

}

Example of using else if statements

Experience this online

The output of the above code will be:

The color is orange

You can change the value of the color variable and check the output yourself by running the above program to see how the if statement works for multiple conditions.

 

Further Reading: The switch case | Java for loop | Do while loop