Scala if else Statements – if, if-else if-else, Nested if-else Statements

Stay updated with the latest technology trends while you're on the move - Join DataFlair's Telegram Channel

1. Scala if else – Objective

Decision making is an important part of any programming language. For this purpose, we have if-else statements. Let’s see how Scala if else works. Before that, you can refer our previous article on Scala Control Statements. We will start with Scala if Statements, Scala if else Statements, Scala if else – if – else statements and nested if else statements with their syntax & examples.

So, let’s start Scala if else Statements.

Scala if else Statements - if, if-else if-else, Nested if-else Statements

Scala if else Statements – if, if-else if-else, Nested if-else Statements

2. Scala if Statement

We begin with the Scala if statement. This statement evaluates an expression. If true, it executes the block of statements under it.

a. Syntax

Take a look at the syntax:

if(Boolean_expression)
{
//Code to execute if expression is true
}

Do you know What is Scala Thread & Multithreading?

If the Boolean expression evaluates to true, the block of code under the if-statement executes. Otherwise, the code right after the code block under the if-statement executes.

Scala if else Statements - if, if-else if-else, Nested if-else Statements

Scala if Statements

b. Scala if Example

Okay, let’s do an example.
object Main extends App

{
    val x=7
    if(x<10)
    {
        println("Woohoo!")
    }
}

And here’s the output:
Woohoo!

Read: Tuples in Scala

3. Scala if-else Statement

What to do if the expression turns out to be false? Execute the code under the else statement.

a. Syntax

For an if-else statement, follow this syntax:

if(Boolean_expression)
{
//Code to execute if expression is true
}
else
{
//Code to execute if expression is false
}
Scala if else Statements - if, if-else if-else, Nested if-else Statements

Scala if else Statements

b. Scala if else example

Let’s take an example.
object Main extends App

{
    val x=17
    if(x<10)
    {
        println("Woohoo!")
    }
    else
    {
        println("Oh no!")
    }
}

And the output is:
Oh no!
Read: Scala Partial Functions

4. Scala if-else if-else Statement

When we want to check another condition when one fails, and yet another when that one fails, and so on, we can use this. One ‘if’ statement can have any number of ‘else if’ statements. But remember to end these with a final ‘else’ statement.

a. Syntax

Follow this syntax:

if(Boolean_expression 1)
{
//Code to execute if expression 1 is true
}
else if(Boolean_expression 2)
{
//Code to execute if expression 2 is true; checked only when expression 1 is false
}
else if(Boolean_expression 3)
{
//Code to execute if expression 3 is true; checked only when expression 2 is false
}
else
{
//Code to execute if expression 3 is false
Scala if else Statements - if, if-else if-else, Nested if-else Statements

Scala if-else if-else Statements

Have a look at – Extractors vs Case Classes

b. Scala if else if else example

Time for an example.
object Main extends App

{
    val x=17
    if(x<10)
    {
        println("Woohoo!")
    }
    else if(x<20)
    {
        println("Okay")
    }
    else
    {
        println("Oh no!")
    }
}

And who’s guessing the output?
Okay
Read: Pros and Cons of Scala

Scala Quiz

5. Scala Nested if-else Statement

You can put an ‘if’ or ‘else if’ statement inside another ‘if’ or ‘else if’ statement. Full flexibility!

a. Syntax

Follow this syntax:

if(Boolean_expression 1)
{
//Code to execute if expression 1 is true
if(Boolean_expression 2)
{
//Code to execute if expression 2 is true
}
else
{
    //Code to execute if expression 2 is false
}
}
else
{
    //Code to execute if expression 1 is false
Scala if else Statements - if, if-else if-else, Nested if-else Statements

Scala Nested if-else Statements

Do you know How Can We Use Scala Throw Keyword?

b. Scala Nested If Else Example

Let’s take an example for this.
object Main extends App

{
    val x=7
    if(x<10)
    {
        println("Woohoo!")
        if(x<5)
        {
            println("Hi")
        }
        else
        {
            println("Bye")
        }
    }
    else
    {
        println("Oh no!")
    }
}

And the output is:
Woohoo!
Bye

So, this was all about Scala If Else Statements. Hope you like our explanation.

6. Conclusion

In this lesson, we learned about Scala if, Scala if else, Scala if-else if-else, and Scala nested if statements with their syntax and examples. Furthermore, if you have any query, feel free to ask in the comment box.

Related Topic – Scala While Loop

Reference

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.