Scala If else Statements

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

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.