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

</>
Copy
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

</>
Copy
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

Conclusion

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