Table of Contents
The logical NOT operator (!) is a unary operator that reverses the logical value of its operand. If the operand evaluates to true, the result is false. If the operand evaluates to false, the result is true.
The logical NOT operator is commonly used to test whether a condition is not satisfied or to invert the value of a Boolean expression.
Source Code #
#include <iostream>
int main()
{
bool is_logged_in = false;
std::cout << "Original Value : " << is_logged_in << '\n';
std::cout << "Negated Value : " << !is_logged_in << '\n';
return 0;
}
Output #
Original Value : 0
Negated Value : 1
Explanation #
- The logical NOT operator (
!) is a unary operator that operates on a single operand. - It reverses the logical value of its operand.
- If the operand is
true, the result becomesfalse. - If the operand is
false, the result becomestrue. !is_logged_inevaluates to the opposite of the value stored inis_logged_in.- The logical NOT operator does not modify the original variable; it only produces the negated result of the expression.
- By default,
std::coutdisplays Boolean values as1fortrueand0forfalse. return 0;terminates the program successfully.