Skip to Content
Logical Operators
Logical NOT Operator

The logical, !, operator is a prefix operator that negates, or reverses, the Boolean value of its operand. It returns false when the operand is true, and returns true, when the operand is false.

Note that unlike the && and || operators, the ! operator acts on a single value.

Assume we have the following variables that hold Boolean values.

let a = true let b = false

Prepending the ! operator to both values will result in the opposite Boolean:

print(!a) // Prints: false print(!b) // Prints: true

The ! operator can also be used to negate the value of an expression as long as that expression is contained within parentheses. The following example first executes the logic within the parentheses and then negates it.

print(!(true && false)) // Prints: true

The resulting value will be true since true && false results in false and !false is true.



In NotOperator.swift, we’ve declared a variable feelingWell and set up an if/else statement that determines a course of action according to how we feel.

Place the following print() statements within their correct code blocks:

  • print("Embrace the day!")
  • print("Have some vitamins and take care of yourself 🤒")
Folder Icon

Take this course for free

Already have an account?