• Articles
  • Tutorials
  • Interview Questions

Learn How to Use If Else Statements in Java to Create Robust and Efficient Code

If Else Statement in Java

The ability to change the behavior of a piece of code that is based on certain information in the environment is known as conditional code flow. The conditional logic in Scala is primarily based on the “if … else” structure.

If Statement in Java

If statement in Java is used to test a condition, if condition is true then the code inside the if statement is executed otherwise that code is not executed.
Syntax

if(Boolean_expression)
{
// Body of if
}

if statement

public class Intellipaat {
public static void main(String args[]){
int i = 20;
if( i == 20 ){
System.out.print("i is equal to 20");
}
}
}

Output
i is equal to 20

If Else Statement in Java

If Else is also used to test a condition, if condition is true then the code inside the if statement is executed otherwise else part is executed.

Syntax

if(Boolean_expression){
// Body of if statement
}else{
// Body of else statement
}

if else statement

e.g.

public class Intellipaat {
public static void main(String args[]){
int i = 40;
if( i == 20 ){
System.out.print("i is equal to 20");
} else{
System.out.print("i is not equal to 20");
}
}
}

Output
i is not equal to 20

If Else If Else Statement in Java

After if statement else if is used to check the multiple conditions.
Syntax

if(Boolean_expression1){
// Body of if statement
}
else if(Boolean_expression2){
//Body of else if
}
else{
// Body of else statement
}

e.g.

public class Intellipaat {
public static void main(String args[]){
int i = 40;
if( i == 20 ){
System.out.print("i is equal to 20");
}
else if(i<50)
{
System.out.print("value of i is less than 50");
}
else{
System.out.print("i is not equal to 20");
}
}
}

Output
value of i is less than 50

Learn Java

Nested If Statement in Java

 It contains multiple if else condition. It is used to check the multiple conditions. This statement is like executing an if statement inside an else statement.
Syntax

if(Boolean_expression1){
//Body of if statement
if(Boolean_expression2)
{
//Body of nested if
}
}
…
else {
//Body of else statement
}

nestedif statement

e.g.

public class Intellipaat {
public static void main(String args[]){
int i = 20;
int j = 10;
if ( i < 30 ){
System.out.println("Value of i is less than 30");
if ( j == 10 ){
System.out.println("Value of j is equal to 10");
}
}
else
{
System.out.println("Value of i is not less than 30");
}
}
}

Output
Value of i is less than 30
Value of j is equal to 10

Get 100% Hike!

Master Most in Demand Skills Now!

Switch Statement in Java

It contains a number of cases with different conditions. When a variable value is matched with the case, then that case is executed.
Syntax

switch(expression){
case value1 :
//Statements
break;
case value2 :
//Statements
break;
case valuen :
//Statements
break;
default : //Optional
//Statements
}

switch

Course Schedule

Name Date Details
Python Course 23 Nov 2024(Sat-Sun) Weekend Batch View Details
30 Nov 2024(Sat-Sun) Weekend Batch
07 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration.