Start learning free

LESSON 88 OF 677 · CONDITIONAL STATEMENTS

Logical Operators: Not (!)

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 often used with Boolean variables:

val 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