To control the execution flow, Java programming language provides two primary conditional constructs: if-else and switch-case statement. In this post we'll talk about if else statement in Java in detail along with usage examples.
The if statement can be used in several different forms depending on the complexity of your logic.
- if statement
- if-else statement
- if-else-if ladder
- nested if statement
Let's go through each one of these with examples
if statement in Java
The if statement in Java is used to test a condition and execute a block of code only if the condition evaluates to true. If the condition is false, the block is skipped.
Syntax of the Java if statement is as follows-
if(condition){
//statement(s)
}
The condition must be a boolean expression that evaluates to either true or false. If the condition evaluates to true then the block of code enclosed in curly braces is executed. If the condition evaluates to false then the if block is skipped.
If there is only a single statement inside the if block, curly braces {} are optional, though using them is considered best practice for readability.
Java if statement flow
Java if statement examples
1- Testing a condition to check if passed number is greater than 5 or not.
public class IfDemo {
public static void main(String[] args) {
int i = 10;
if(i > 5) {
System.out.println("Value of i is greater than 5");
}
System.out.println("After if statement");
}
}
Output
Value of i is greater than 5 After if statement
2- You can also use conditional operators like Conditional-AND (&&) and Conditional-OR (||) to create a condition.
public class IfDemo {
public static void main(String[] args) {
int i = 10;
String test = "Hello";
if(i > 5 && test.equals("Hello"))
System.out.println("Inside if");
System.out.println("After if statement");
}
}
Output
Inside if After if statement
Java if-else statement
The if else statement in Java allows you to define an alternate execution path when the condition evaluates to false.
Syntax of the Java if-else statement is as follows-
if(condition){
// if block
}else{
// else block
}
Here condition is a boolean expression that evaluates to either true or false.
- If the condition evaluates to true then if block is executed.
- If the condition evaluates to false then the else block is executed.
Java if-else statement flow
Java if-else statement examples
public class IfDemo {
public static void main(String[] args) {
int i = 10;
String test = "Hello";
if(i > 20 && test.equals("Hello")) {
System.out.println("Inside if");
}else {
System.out.println("Inside else");
}
System.out.println("After if-else statement");
}
}
Output
Inside else After if-else statement
In the example condition fails therefore else block is executed.
Java if-else-if ladder
The if‑else‑if ladder in Java is used when you need to evaluate multiple conditions sequentially. It allows you to chain several if and else if statements together, followed by an optional else block. Each if and else-if statement has a condition and a particular block is executed if the condition associated with that block evaluates to true. If none of the conditions evaluates to true then the else block (if present) runs.
Syntax of Java if-else-if syntax is as follows-
if(condition1){
// statements for condition1;
}else if(condition2){
// statements for condition2
}else if(condition3){
// statements for condition3
}
.
.
.
else{
// statements if none of the above conditions are true
}
Java if-else-if examples
Suppose you have requirement to add 10% to amount if amount is greater than 5000.
Add 15% if amount is more than 3000 but less than or equal to 5000.
Add 20% if amount is more than 1000 but less than or equal to 3000.
Otherwise add 25% to the amount.
public class IfDemo {
public static void main(String[] args) {
int amount = 5000;
if(amount > 5000) {
// add 10%
amount = amount + (amount*10/100);
}else if (amount > 3000 && amount <= 5000) {
// add 15%
amount = amount + (amount*15/100);
}else if (amount > 1000 && amount <= 3000) {
// add 20%
amount = amount + (amount*20/100);
}else {
//add 25%
amount = amount + (amount*25/100);
}
System.out.println("Amount is- " + amount);
}
}
Output
Amount is- 5750
Java nested if-else statements
It is possible to have an if-else statement inside another if-else statement in Java. It is known as a nested if-else statement. It is particularly useful when you need to perform hierarchical decision‑making, where one condition depends on the outcome of another.
Syntax of Nested if‑else in Java
if (condition1) {
if (condition2) {
// statements executed if both condition1 and condition2 are true
} else {
// statements executed if condition1 is true but condition2 is false
}
} else {
// statements executed if condition1 is false
}
In the given program, the nested if‑else statement in Java is used to apply different bonus percentages to the amount based on multiple conditions. There is a first level check to verify if the amount is greater than 5000 or not, with in that there is a nested if-else to check for other ranges.
public class IfDemo {
public static void main(String[] args) {
int amount = 8000;
if(amount > 5000) {
if(amount > 7000 && amount <=10000) {
amount = amount + (amount*10/100);
}else {
amount = amount + (amount*5/100);
}
}else {
if (amount > 3000 && amount <= 5000) {
amount = amount + (amount*15/100);
}else {
amount = amount + (amount*20/100);
}
}
System.out.println("Amount is- " + amount);
}
}
Output
Amount is- 8800
That's all for this topic if-else Statement in Java With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Java Basics Tutorial Page
Related Topics
You may also like-





