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 the basics of OOPs in Java through this blog!

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

If you want to learn Control Statements in Java, refer to our Java blog!

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
Learn more about Cross-Platform Mobile Technology for Android and iOS using Java in this insightful blog now!

Course Schedule

Name Date Details
Python Course 09 Mar 2024(Sat-Sun) Weekend Batch
View Details
Python Course 16 Mar 2024(Sat-Sun) Weekend Batch
View Details
Python Course 23 Mar 2024(Sat-Sun) Weekend Batch
View Details

Leave a Reply

Your email address will not be published. Required fields are marked *