If statements

Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this.

The if Statement

The if statement is used to execute some code only if a specified condition is true.

Syntax

The if…else Statement

The if…else statement is used to execute some code if a condition is true and another code if the condition is false.

Syntax

Run The Application

Right click to the IfElseDemo class; select Run As -> Java Application.

Output

The if…elseif….else Statement

Use the if….elseif…else statement to specify a new condition to test, if the first condition is false.

Syntax

We modify the example above:

Run The Application

Right click to the IfElseIfElseDemo class; select Run As -> Java Application.

Output