Java if – Using if, if else and else if statements in Java with examples

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 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 Java switch chapter.

Java if statement

The if condition is evaluated by the Boolean variables.

The general Syntax of if statement in Java:

If (condition) {

    // Code to be executed if condition is true

}

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 be executed. If the condition is false, the statement(s) after closing curly braces will be executed.

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.

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.

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 examples 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 java else block should be executed. For that, you can use else if Java statements, to check more than two options in the if block.

Syntax of using 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 Java if statement works for multiple conditions.

 

Also see – The switch case | Java for loop | Do while loop

Was this article helpful?

Related Articles

Leave A Comment?