Logical NOT (!
) Operator in C++
The Logical NOT operator in C++ is a unary operator (operates on one operand) that reverses the boolean value of an expression.
- If the condition is
true
,!
makes itfalse
. - If the condition is
false
,!
makes ittrue
.
This operator is commonly used to negate conditions and write more concise control structures.
Syntax
!condition
Truth Table
Condition | !Condition |
---|---|
false | true |
true | false |
Example 1: Basic Usage
not_basic.cpp
bool isAvailable = true;
std::cout << !isAvailable; // Output: 0
Explanation:
isAvailable
istrue
!isAvailable
becomesfalse
→0
is printed
Example 2: Negating a Comparison
not_comparison.cpp
int age = 15;
std::cout << !(age >= 18); // Output: 1
Explanation:
age >= 18
→ false!false
→ true → Output:1
Example 3: In an If Statement
not_if.cpp
bool isLoggedIn = false;
if (!isLoggedIn) {
std::cout << "Please log in first.";
}
Explanation:
- Since
isLoggedIn
is false,!isLoggedIn
becomes true → message is shown
Example 4: With Integers
In C++, integer values can be used in logical expressions:
0
is treated asfalse
- Any non-zero value is
true
not_integer.cpp
int x = 0;
std::cout << !x; // Output: 1
Explanation:
x
is 0 (false), so!x
becomes true → Output:1
Example 5: With Characters
not_char.cpp
char c = 'A';
std::cout << !c; // Output: 0
Explanation:
'A'
has an ASCII value of 65 (non-zero, true)!c
→ false → Output:0
Example 6: Complex Condition
not_complex.cpp
int score = 90;
std::cout << !(score < 40 || score > 100); // Output: 1
Explanation:
score < 40 || score > 100
→ false!false
→ true → Output:1
Example 7: Not in Loops
not_loop.cpp
bool isDone = false;
while (!isDone) {
std::cout << "Processing...\n";
isDone = true;
}
Explanation:
- The loop runs once because
!isDone
is initiallytrue
Summary
- The
!
operator is used to reverse a Boolean value. - It’s most useful in
if
,while
, and conditional expressions. - Can be used on
bool
, integers, characters, and expressions that evaluate to Boolean logic. - Keep your expressions readable when using
!
with complex conditions.
Practice Exercises
- Write a program to print “Access Denied” if a user is not authenticated (
isAuthenticated = false
). - Create a program that prints “Outside range” if a number is not between 10 and 20.
- Use the
!
operator in awhile
loop to keep prompting the user until a condition becomes true.