!, 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
Note that unlike the
|| operators, the
! operator acts on a single value.
Assume we have the following variables that hold Boolean values.
let a = true let b = false
! operator to both values will result in the opposite Boolean:
print(!a) // Prints: false print(!b) // Prints: true
! 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 && false results in
In NotOperator.swift, we’ve declared a variable
feelingWell and set up an
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 🤒")