Scala If, If-Else, If-Else-If

Conditional Statements are used to execute a block of statements based on the value of a boolean expression.

Scala provides following conditional statements.

  1. if statement
  2. if-else statement
  3. if-else-if statement

Scala IF Statement

The syntax of Scala if statement is

if(boolean_expression) {
     // statements inside if block
 }

Scala if statement contains an if keyword followed by a boolean expression.

If the boolean expression evaluates to true, then the statements inside the if block are executed. If the boolean expression evaluates to false, the statements inside the if block are not executed. After executing the if block, the program control continues with the statements after the if block.

Following are some of the examples of boolean expression and their resulting value.

  • (8>6) true
  • (true && true) true
  • (9%2==1) true
  • (4>8) false
  • (true && false) false

Example 1 – Scala IF statement

In the following example, we have an if statement where it checks if a is an even number. If the condition evaluates to true, it prints a string.

example.scala

object IfExample {
  def main(args: Array[String]) {
    var a=24
    if (a%2==0){
      println(a+" is an even number");
    }
  }
}

Output

24 is an even number

Scala IF-ELSE Statement

The syntax of Scala if-else statement is

if(boolean_expression) {
     // statements inside if block
 } else {
     // statements inside else block
 }

Scala if-else statement contains a boolean expression, if block, and else block.

If the boolean expression evaluates to true, then the statements inside the if block are executed. If the boolean expression evaluates to false, the statements inside the else block are executed.

Example 2 – Scala if-else statement

In this example, we have a boolean expression where it checks if a is an even number. If the condition evaluates to true, it prints a that the number is even, else it prints that the number is odd.

example.scala

object IfElseExample {
  def main(args: Array[String]) {
    var a=27
    if (a%2==0){
      println(a+" is an even number");
    } else {
      println(a+" is an odd number");
    }
  }
}

Output

27 is an odd number

You may change the value of a, and run this program to understand the working of if-else statement in Scala.

Scala ELSE-IF Statement

The syntax of Scala if-else-if statement is

if(boolean_expression_1) {
     // statements
 } else if(boolean_expression_2) {
     // statements
 } else if(boolean_expression_3) {
     // statements
 } else {
     // statements inside else block
 }

Scala else-if statement contains a boolean expression if and else-if blocks.

The program control first evaluates the boolean_expression_1. If the boolean_expression_1 evaluates to true, then the block of statements next to the expression is evaluated. If boolean_expression_1 evaluates to false, then boolean_expression_2 is evaluated. If it evaluates to true, then the statements in the block right after the expression are evaluated. And if boolean_expression_2 evaluates to false, it continues with the next boolean expression the if-else-if ladder.

When an expression evaluates to true, the corresponding block is executed and the program control jumps to the end of if-else-if ladder.

When no boolean expression in the if-else-if evaluates to true, the optional else block at the end gets evaluated.

Example 3 – Scala if-else-if statement

In this example, we have a series of boolean expressions where a number is checked for divisibility.

example.scala

object ElseIfExample {
  def main(args: Array[String]) {
    var a=27
    if (a%2==0){
      println(a+" is divisible by 2")
    } else if(a%3 ==0){
      println(a+" is divisible by 3")
    } else if(a%4 ==0){
      println(a+" is divisible by 4")
    } else if(a%5 ==0){
      println(a+" is divisible by 5")
    }
  }
}

Output

27 is divisible by 3

You may change the value of a, and run this program to understand the working of if-else-if statement in Scala.