Scala – IF ELSE Statements

  • Post category:Scala
  • Reading time:5 mins read

IF-ELSE

A great thing about the Scala if construct is that it always returns a result. You can ignore the result as we did in the previous examples, but a more common approach — especially in functional programming — is to assign the result to a variable.

This chapter takes you through If construct in Scala programming.

Flow Chart

The following is a flow chart diagram for If conditional statement.

Scala If-Else Construct

If Statement

‘if’ statement consists of a Boolean expression followed by one or more statements.

Syntax

The syntax of an ‘if’ statement is as follows.

if(Boolean Expression) {
   // Statements will execute if expression evaluates to true.
}

The block of code inside the ‘if’ expression will execute when the boolean expression evaluates to true. If not, the first set of code after the end of the ‘if’ expression ( after the closing curly brace ) will execute.

Example

object Demo extends App {
   var x = 5;
   if( x > 1 ){
      println("Input value is greater than 1");
   }
}

Output

Input value is greater than 1

If-else Statement

An ‘if’ statement can be followed by an optional else statement, which executes when the boolean expression evaluates to false.

Syntax

if(Boolean Expression){
   // Statements will execute if expression evaluates to true.
} else{
   // Statements will execute if expression evaluates to false.
}

Example

object Demo extends App{
   var value = 100;

   if( value < 50){
      println("Input value is less than 50");
   } else {
      println("Input value is greater than 50");
   }
}

Output

Input value is greater than 50

If-else-if-else Statement

An ‘if’ statement can be followed by an optional ‘else if…else‘ statement, which is very useful to test multiple conditions.

Syntax

if(Boolean Expression 1){
   // Statements will execute if expression 1 evaluates to true.
} else if(Boolean Expression 2){
   // Statements will execute if expression 2 evaluates to true.
} else if(Boolean Expression 3){
   // Statements will execute if expression 3 evaluates to true.
} else {
   // Executes when the none of the above condition is true.
}

Example

object Demo {
   def main(args: Array[String]) {
      var value = 100;

      if( value == 10 ){
         println("Input value is 10");
      } else if( value == 20 ){
         println("Input value is 20");
      } else if( value == 30 ){
         println("Input value is 30");
      } else{
         println("Input value does't match any of the If-Else-If");
      }
   }
}

Output

Input value does't match any of the If-Else-If

Nested If-Else

It is always legal to nest if-else statements

Syntax

if(Boolean Expression 1){
   // Executes when the Boolean expression evaluates to true
   
   if(Boolean Expression 2){
      // Executes if the nested Boolean expression evaluates to true
   }
}

Example

object Demo extends App{
   var x = 100;
   var y = 101;
      
   if( x == 100 ){
      if( y > 100 ){
         println("x is equal to 100 AND y is greater than 100");
      }
   }
}

Output

x is equal to 100 AND y is greater than 100