• Home
  • 3.12 Logical NOT Operator (!)

3.12 Logical NOT Operator (!)

View Categories

3.12 Logical NOT Operator (!)

1 min read

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 becomes false.
  • If the operand is false, the result becomes true.
  • !is_logged_in evaluates to the opposite of the value stored in is_logged_in.
  • The logical NOT operator does not modify the original variable; it only produces the negated result of the expression.
  • By default, std::cout displays Boolean values as 1 for true and 0 for false.
  • return 0; terminates the program successfully.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *