bing
Flat 10% & upto 50% off + Free additional Courses. Hurry up!

Java If..Else Statements

 

The ability to change the behavior of a piece of code which 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

If statement 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

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

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

 

Nested If Statement

 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

 

Switch Statement

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!

"0 Responses on Java If..Else Statements"

Training in Cities

Bangalore, Hyderabad, Chennai, Delhi, Kolkata, UK, London, Chicago, San Francisco, Dallas, Washington, New York, Orlando, Boston

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.

top

Sales Offer

  • To avail this offer, enroll before 10th December 2016.
  • This offer cannot be combined with any other offer.
  • This offer is valid on selected courses only.
  • Please use coupon codes mentioned below to avail the offer
offer-june

Sign Up or Login to view the Free Java If..Else Statements.