Swift If Statement with NOT Operator

To use logical NOT operator in If statement use the symbol ! before the condition or boolean expression.

The syntax to use NOT logical operator with If statement is

if ( !condition ) {
    //code
}

Example

In the following program, we will check if the number is not less than 5, using logical NOT operator in if boolean expression.

main.swift

var n = 9

if ( !(n < 5) ) {
    print("n is not less than 5.")
} else {
    print("n is less than 5")
}

Output

Swift If Statement with NOT Operator
ADVERTISEMENT

Conclusion

In this Swift Tutorial, we learned how to use logical NOT Operator in If statement’s boolean expression.