Start learning free

LESSON 82 OF 414 · CONDITIONAL STATEMENTS

Chaining Conditionals in Scala: else if

Lesson Read the full explanation

Sometimes we can have more than two possible scenarios which need a different code to be ran. We can add else if statements to allow for as many outcomes as we want:

var language = "Python"
if(language == "Scala") {
    println("It's super popular!")
}
else if(language == "Python") {
    println("It's so simple!")
}
else if (language == "Java") {
    println("It's pretty hardcore!")
}
else {
    println("It's exotic!")
}

In the example above, language == "Scala" evaluates to false, so we move on and enter the following else if.

There, language == "Python" evaluates to true, so we execute what's inside that block and print "It's so simple!".

The rest of the conditions are not evaluated.

In case none of the conditions evaluates to true, then the code in the final else statement would execute. For example, if language was "Go", the program would have printed "It's exotic!"


Assignment
Follow the Coding Tutorial and let's write some chained conditionals.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 4
    Ctrl + Enter to check

    SCALA · CONDITIONAL STATEMENTS

    Chaining Conditionals in Scala, explained

    When two branches are not enough

    An if with an else splits the program into two paths. Often there are more than two cases: a grade can be failing, passing, good or perfect. Adding else if branches lets one chain handle as many outcomes as you need.

    val language = "Python"
    if (language == "Scala") {
        println("It's super popular!")
    } else if (language == "Python") {
        println("It's so simple!")
    } else if (language == "Java") {
        println("It's pretty hardcore!")
    } else {
        println("It's exotic!")
    }
    

    The chain is checked from the top. language == "Scala" is false, so the program moves on. language == "Python" is true, so it runs that block and prints It's so simple!. The remaining conditions are never evaluated. If none of the conditions is true, the final else runs. With language set to "Go" the output would be It's exotic!.

    Rules of the chain

    • Exactly one block runs when the chain ends with an else. At most one runs when it does not.
    • Order matters. The first condition that is true wins, and everything below it is skipped.
    • Each later condition can rely on the earlier ones being false. That is why you can write fewer comparisons, as the exercise shows below.
    • The final else is optional and has no condition. It catches every case the branches above did not.

    Order mistakes

    Because the first match wins, broad conditions placed too early hide the narrow ones. Take this version of a grade check:

    def wrong(grade: Int): String =
        if (grade <= 9) "Good Job"
        else if (grade <= 7) "Passed the test"
        else "Perfect"
    
    println(wrong(6))
    
    Good Job
    

    A grade of 6 is also at most 9, so the first branch catches it and the second can never run. Put the most restrictive condition first, as in the lesson's chain, which starts from the lowest grades and moves up.

    Chains are expressions

    In Scala, if and else produce a value, so a chain can be returned from a method or bound to a val instead of printing in each branch:

    def label(grade: Int): String =
        if (grade < 5) "Failed the test"
        else if (grade <= 7) "Passed the test"
        else if (grade <= 9) "Good Job"
        else "Perfect"
    

    Here the final else is what guarantees a String in every case. An if without else produces the empty value () of type Unit when its condition is false, so it cannot stand in for a String. The lesson's exercise only prints, so it uses blocks with println.

    The lesson's exercise

    The code starts with val grade = 8. The task has three steps.

    1. Start the chain with an if. It prints "Failed the test" when grade < 5. Failing grades are handled before anything else.
    2. Add two else if branches. The first prints "Passed the test" when grade <= 7. The second prints "Good Job" when grade <= 9. "Up to 7" includes 7, so the comparisons use <=. The first branch to match wins. A grade that reaches the first else if is already known to be at least 5, so there is no need to write grade >= 5 && grade <= 7.
    3. Finish with an else. It prints "Perfect" for every grade above 9.
    val grade = 8
    
    if (grade < 5) {
        println("Failed the test")
    } else if (grade <= 7) {
        println("Passed the test")
    } else if (grade <= 9) {
        println("Good Job")
    } else {
        println("Perfect")
    }
    

    With grade = 8, the first test (8 < 5) and the second (8 <= 7) are false, and the third (8 <= 9) is true, so the program prints Good Job. The boundaries behave like this:

    gradeoutput
    3Failed the test
    5Passed the test
    7Passed the test
    9Good Job
    10Perfect

    Common mistakes

    • Writing elseif or elif. Scala spells it as two words: else if.
    • Using separate if statements. Independent ifs all run their checks, so a grade of 8 could print more than one message. Chain them with else if when the cases exclude each other.
    • Off-by-one boundaries. Writing grade < 7 instead of grade <= 7 would send a 7 to the "Good Job" branch.

    Key takeaways

    • An if / else if / else chain picks the first branch whose condition is true.
    • Later branches only run when every earlier condition was false.
    • Put narrower conditions before broader ones, and finish with an else for everything else.
    • A chain is an expression in Scala, so it can produce a value.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free