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

Scala 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 statemente.g.

object Intellipaat {

def main(args: Array[String]) {

var I = 20;

if( I==20 ){

println("If statement is executed");

}

}

}

Compile and execute the above program:

scalac Intellipaat.scala

scala Intellipaat

Output

If statement is executed

 

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.

object Intellipaat {

def main(args: Array[String]) {

var I = 20;

if( I<=20 ){

println("If statement is executed");

}

else

{

println("Else statement is executed");

}

}

}

Compile and execute the above program:

scalac Intellipaat.scala

scala Intellipaat

Output

If statement is executed

 

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.

object Intellipaat {

def main(args: Array[String]) {

var I = 20;

if( I<20 ){

println("If statement is executed");

}

else if (I ==20)

{

println("Else if statement is executed");

}

else

{

println("Else statement is executed");

}

}

}

Compile and execute the above program:

scalac Intellipaat.scala

scala Intellipaat

Output

Else if statement is executed

 

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

}

e.g.

object Intellipaat {

def main(args: Array[String]) {

var I = 20;

if( I<=20 ){

println("If statement is executed");

if(i<30)

{

println("Nested if statement is executed");

}

}

else

{

println("Else statement is executed");

}

}

}

Compile and execute the above program:

scalac Intellipaat.scala

scala Intellipaat

Output

If statement is executed

Nested if statement is executed

 

Living without break and continue

Scala does not use break and continue command because these commands are not mesh well with function literals. The simple approach is to replace every continue statement by if statement and every break statement by a boolean variable. The Boolean variable indicates whether the enclosing while loop should continue.

"0 Responses on Scala 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 07th 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 Scala if..else Statements.