In Java, to check any certain condition whether it is true or false we use If-Else Statement in Java.
If-Else Statement in Java checks boolean condition i.e True or False. There are mainly three types of statements:
- if statement
- if-else statement
- if-else-if
- nested if statement
1. Java if Statement:
Let’s suppose I have to check a number which is equal to 5. Then I will use if statement in Java. If my condition is true then code will execute written inside if statement.
Syntax:
if(condition){
//code to be executed
}
Example:
public class DemoClass {
public static void main(String[] args) {
int age = 20;
// checking the age
if (age > 17) {
System.out.print("Age is greater than 17");
}
}
}
2. If-else Statement in Java:
if-else statement in Java is very similar to if statement. The difference is if any condition meets then if block will be executed otherwise else block will be executed.
Syntax:
if(condition){
//code will be executed if condition is true
}else{
//code will be executed if condition is false
}
Example:
public class DemoClass {
public static void main(String[] args) {
int age = 20;
// checking the age
if (age > 18) {
System.out.print("You're not under age.");
} else {
System.out.print("You're under age.");
}
}
}
3. Java if-else-if Statement:
Java if else condition is a ladder-type statement where code will be executed after multiple condition check. That means you can check multiple conditions using this statement.
Syntax:
public class DemoClass {
public static void main(String[] args) {
if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition2 is true
} else if (condition3) {
// code to be executed if condition3 is true
} else {
// code to be executed if all the conditions are false
}
}
}
Example:
public class DemoClass {
public static void main(String[] args) {
int age = 20;
// checking the age
if (age < 10) {
System.out.println("You are below 10 years");
}
else if (age >= 10 && age < 20) {
System.out.println("You are between 10 & 20 years");
}
else if (age >= 20 && age < 30) {
System.out.println("You are between 20 & 30 years");
}
else if (age >= 30 && age < 40) {
System.out.println("You are between 30 & 40 years");
}
else {
System.out.println("You're above 40 year");
}
}
}
4. Java nested if Statement:
The nested if statement represents if block inside another if block. Nested if block will execute only when outer if block is true.
Syntax:
if(condition){
//code to be executed
if(condition){ //nested
//code to be executed
}
}
Example of nested if statement:
public class DemoClass {
public static void main(String[] args) {
int age = 20;
int weight = 70;
// applying condition
if (age >= 18) {
if (weight > 50) {
System.out.println("You can donate blood");
}
}
}
}
Example of nested if-else statement:
public class DemoClass {
public static void main(String[] args) {
int age = 20;
int weight = 70;
// applying condition
if (age >= 18) {
if (weight > 50) {
System.out.println("You can donate blood");
} else {
System.out.println("You're not eligible to donate blood");
}
}
}
}
Frequently Asked Questions:
1. Can you have multiple if statements in Java?
Yes, we can write multiple if statement in Java.
2. Multiple if statement or single if-else-if statement?
If you write multiple if statement then all conditions will be checked and executed but if you write single if-else-if statement then only one condition is executed.
Official Docs: Visit Here.
I hope you’ve understood If-Else statement in Java and their different types.