
A Java if-else statement performs a task, depending on whether the given condition is true or false. Here task is a single statement or group of statements.
In other words, The if-else statement tells the program to execute a specific section of code only if the particular condition is true.
It allows the computer to first test the expression and then, depending on whether the condition is true or false, it transfers the control to a particular statement. So you have to go through the two paths, one for true condition and other for false condition (If available).

Table of Contents
What is control statement?
The control statement changes the flow of execution and provides better control to program on the flow of execution.
The control statement, which enables you to execute different block of code based on a simple condition in Java. It is nearly identical to if statements in C or C++. If condition contain the keyword if, followed by a boolean condition, followed by a statement to execute if the condition is true.
Types of If-else statement
There are many ways to use if statement in Java language which are as follows:
- if statement
- if-else statement
- if-else-if ladder or else-if statement
- nested if statement
1. If statement
In this statement, there is only one condition, If the condition is true then the statement inside the If will execute. This is the most basic statement.
You must put the condition inside the parentheses (<condition>).
if (condition) {
// Code to be executed if the condition is true
}
2. Java if-else statement
In this statement, If condition-1 is true, then code inside the condition-1 will execute, if condition-1 is false then code inside else will execute.
if (condition-1) {
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}
3. Java if-else-if ladder or else-if statement
In this statement, if condition-1 is true, then code inside condition-1 will execute, if condition-1 is false then condition-2 will be checked if it is true then code inside condition-2 will execute. If condition-2 is false then condition-3 will be checked if it is true then code inside the condition-3 will execute otherwise else part will be executed.
if (condition-1) {
// Code to be executed if the condition 1 is true
} else if (condition-2) {
// Code to be executed if the condition 1 is false and condition 2 is true
} else if (condition-3) {
// Code to be executed if the condition 1 is false and condition 2 is false and condition 3 is true
} else {
// Code to be executed if the condition 1, condition 2 and condition 3 are false
}
4. Java nested if statement
The nested if statement means a if statement inside the if statement. In other words, if the condition-1 is true then condition-2 will be checked, if condition-2 is true then code inside the condition-2 will be executed.
if (condition-1) {
if (condition-2) {
// Code to be executed if the condition 1 is true and condition 2 is true
}
}
In this section, we have used the word “statement”. The statement represents a single time execution from top to bottom.
The difference between if condition in Java and C or C++ is that Java condition must return a boolean value (true or false). Unlike in C, the condition cannot return an integer.
You can use Relational Operators to perform different actions for different decisions.