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; } |
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:
- Logical AND ( && ) Operator
- Logical OR ( || ) Operator
- Logical NOT ( ! ) Operator
Let’s discuss each of the operators in detail.
Contact Us