Start learning free

LESSON 78 OF 348 · CONDITIONAL STATEMENTS

Logical Operators: Not (!) in Scala

Lesson

The logical not operator (!) is used to invert the value of a Boolean condition. For example:

7 <= 10 // Evaluates to true
!(7 <= 10) // Evaluates to false

12 != 12 // Evaluates to false
!(12 != 12) // Evaluates to true

!true // Evaluates to false

It is oftenly used with Boolean variables:

bool hungry = true
if(!hungry) {
    println("I can wait a little longer")
}
else {
    println("I need food!")
}

!hungry evaluates to false, so we enter the else and print "I need food!".

Assignment
Follow the Coding Tutorial and play with the not operator.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    Not Operator in Scala

    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free