In this tutorial, you shall learn how to use NOT logical operator for the condition of If-statement in Kotlin, with examples.
Kotlin If with NOT Operator
The condition in If-statement can be negated using Logical NOT Operator !.
The following is a simple scenario where the condition is negated with Logical NOT Operator.
</>
Copy
if ( !condition ) {
//code
}
The truth table for the result of the expression !condition is
| condition | !condition |
|---|---|
| true | false |
| false | true |
Example
In the following Kotlin program, we have an integer value in a. We shall check if this integer is not even.
The expression to check if a is even is a%2 == 0.
The expression to check if a is not even is !(a%2 == 0).
Main.kt
</>
Copy
fun main(args: Array<String>) {
val a = 13
if ( !(a%2 == 0) ) {
print("$a is not even.")
}
}
Output
13 is not even.
Conclusion
In this Kotlin Tutorial, we learned how to use Logical NOT Operator in If-statement’s boolean condition, with the help of example programs.
