Logical NOT Operator ( ! )

The C++ logical NOT operator ( ! ) is a unary operator that is used to negate the value of a condition. It returns true if the condition is false, and false if the condition is true. Here’s the truth table for the NOT operator:

Operand 1

Result

true

false

false

true

Syntax of Logical NOT

! expression

Example of Logical NOT in C++

C++




// C++ program to illustrate the logical not operator
#include <iostream>
using namespace std;
  
int main()
{
  
    bool isLoggedIn = false;
  
    // using logical not operator
    if (!isLoggedIn) {
        cout << "Please log in to access this feature."
             << endl;
    }
    else {
        cout << "Welcome to w3wiki!" << endl;
    }
  
    return 0;
}


Output

Please log in to access this feature.

Explanation: In the above code, the condition ‘!isLoggedIn’ checks whether the user is not logged in. If the condition is true (i.e., the user is not logged in), the message “Please log in to access this feature.” will be displayed otherwise else statement will be printed.

C++ Logical Operators

In C++ programming languages, logical operators are symbols that allow you to combine or modify conditions to make logical evaluations. They are used to perform logical operations on boolean values (true or false).

In C++, there are three logical operators:

  1. Logical AND ( && ) Operator
  2. Logical OR ( || ) Operator
  3. Logical NOT ( ! ) Operator

Let’s discuss each of the operators in detail.

Similar Reads

1. Logical AND Operator ( && )

The C++ logical AND operator (&&) is a binary operator that returns true if both of its operands are true. Otherwise, it returns false. Here’s the truth table for the AND operator:...

2. Logical OR Operator ( || )

...

3. Logical NOT Operator ( ! )

The C++ logical OR operator ( || ) is a binary operator that returns true if at least one of its operands is true. It returns false only when both operands are false. Here’s the truth table for the OR operator:...

Conclusion

...

Contact Us