Start learning free

LESSON 70 OF 414 · CONDITIONAL STATEMENTS

Comparison Operators in Scala: ==, !=, <, >, <=, >=

Lesson Read the full explanation

When writing if statements, we usually compare two values using one or more comparison operators:

Operator Name Examples
== Equal  x == 5,  x == y
!= Not equal  x != 3,  x != y
> Greater than  x > 5,  x > y
< Less than  x < 8,  x < y
>= Greater than or equal to  x >= 4,  x >= y
<= Less than or equal to  x <= 3,   x <= y

For example:

10 == 10 // Evaluates to true
3 < 2 // Evaluates to false
15 >= 15 // Evaluates to true
17 != 12 // Evaluates to true

We usually use these operators with variables:

var a = 10
var b = 15

if (a == b) {
    println("a is equal to b")
}
if (a < b) {
    println("a is less than b")
}

The first condition is not satisfied since 10 == 15 evaluates to false.

The second condition is satisfied since 10 < 15 evaluates to true.

The code above prints "a is less than b".


Assignment
Follow the Coding Tutorial and let's play with comparison operators.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 3
    Ctrl + Enter to check

    SCALA · CONDITIONAL STATEMENTS

    Comparison Operators in Scala, explained

    What comparison operators do

    An if statement needs a Boolean condition, and most of the time that condition comes from comparing two values. A comparison operator looks at the values on both sides and gives back true or false.

    OperatorMeaningExamples
    ==equal tox == 5, x == y
    !=not equal tox != 3, x != y
    >greater thanx > 5, x > y
    <less thanx < 8, x < y
    >=greater than or equal tox >= 4, x >= y
    <=less than or equal tox <= 3, x <= y

    Each of these is an expression with a value:

    10 == 10 // true
    3 < 2 // false
    15 >= 15 // true
    17 != 12 // true
    

    Comparisons inside if statements

    Usually you compare variables:

    var a = 10
    var b = 15
    
    if (a == b) {
        println("a is equal to b")
    }
    if (a < b) {
        println("a is less than b")
    }
    

    The first condition is 10 == 15, which is false, so its block is skipped. The second is 10 < 15, which is true. The program prints a is less than b.

    Scala details

    • == compares values. For numbers and strings it asks whether they are equal, not whether they are the same object. "abc" == "ab" + "c" is true. Use == for strings; there is no need for equals.
    • Different number types compare by value. 5 == 5.0 is true, with an Int on the left and a Double on the right.
    • Strings can be ordered. "apple" < "banana" is true, because strings compare alphabetically by character code.
    • Be careful with decimals. Doubles are stored in binary, so 0.1 + 0.2 == 0.3 is false. To compare calculated decimals, check that the difference is tiny, as in math.abs(x - y) < 1e-9.
    • A single = is not a comparison. It assigns. if (grade = 5) doesn't compile.

    Storing a comparison in a variable

    Because a comparison is an ordinary expression, you can keep its result in a val and use it later, or print it directly:

    val grade = 4
    val failed = grade < 5
    println(failed)
    println(grade <= 4)
    println(grade > 4)
    

    This prints:

    true
    true
    false
    

    The variable failed has the type Boolean, so if (failed) works exactly like writing the comparison inside the parentheses. Naming a condition this way is useful when it is long or used more than once, and a good name such as failed or isAdult makes the if read almost like a sentence.

    The exercise

    The starter code gives val grade = 8. The task prints whether the student passed, following the rule that a grade below 5 is a fail.

    1. Write the condition. A failed test means grade < 5. The operator < is true only when the left side is strictly smaller, so a grade of exactly 5 is not a fail.
    2. Print inside the if. Use println("Failed the test").
    3. Add the else. It takes no condition and covers every grade of 5 or more, so it prints "Passed the test".
    val grade = 8
    
    if (grade < 5) {
        println("Failed the test")
    } else {
        println("Passed the test")
    }
    

    With a grade of 8 the condition 8 < 5 is false, so the program prints Passed the test. A grade of 4 prints Failed the test, and a grade of exactly 5 prints Passed the test.

    Common mistakes

    • Choosing <= instead of <. With grade <= 5, a grade of 5 would count as a fail. Check the boundary value whenever you pick an operator.
    • Swapping the sides. 5 < grade is a different test from grade < 5.
    • Writing =< or =>. The equal sign comes second: <= and >=. In Scala, => means something else entirely, so it won't work as a comparison.

    Key takeaways

    • Comparison operators produce a Boolean, so they fit directly in an if condition.
    • == and != test equality; <, >, <= and >= test order.
    • In Scala, == compares values, strings included.
    • Test the boundary value to decide between < and <=.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free