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.
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check