Logical NOT Operator ( ! )

The logical NOT operator ( ! ) is a unary operator that is used change the boolean value. 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

Below is the implementation of the above method:

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;
}


C




#include <stdio.h>
#include <stdbool.h>
  
int main() {
  
    bool isLoggedIn = false;
  
    // using logical not operator
    if (!isLoggedIn) {
        printf("Please log in to access this feature.");
    }
    else {
        printf("Welcome to w3wiki!");
    }
    
    return 0;
}


Java




/*package whatever //do not write package name here */
  
import java.io.*;
  
class GFG {
    public static void main (String[] args) {
        
    boolean isLoggedIn = false;
  
      // using logical not operator
      if (!isLoggedIn) {
          System.out.println("Please log in to access this feature.");
      }
      else {
          System.out.println("Welcome to w3wiki!");
      }
        
    }
}


Python3




isLoggedIn = False;
  
# using logical not operator
if not(isLoggedIn):
  print("Please log in to access this feature.")
else:
  print("Welcome to w3wiki!")


Javascript




let isLoggedIn = false;
  
// using logical not operator
if (!isLoggedIn) {
    console.log("Please log in to access this feature.");
}
else {
    console.log("Welcome to w3wiki!");
}


Output

Please log in to access this feature.

Explanation: In the 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.

Boolean Data Type

In programming languages, we have various data types to store different types of data. Some of the most used data types are integer, string, float, and boolean. The boolean data type is a type of data that stores only two types of values i.e. True or False. These values are not case-sensitive depending upon programming languages. The name Boolean comes from the branch of mathematics called Boolean algebra, named after George Bool the mathematician.

Similar Reads

What is Boolean Data Type?

The boolean data type is used to store logic values i.e. truth values which are true or false. It takes only 1 byte of space to store logic values. Here, true means 1, and false means 0. In the boolean data type any value other than ‘0’ is considered as ‘true’. Boolean values are most commonly used in data structures to decide the flow of control of a program and decision statements.In programming languages, we have various data types to store different types of data. Some of the most used data types are integer, string, float, and boolean. The boolean data type is a type of data that stores only two types of values i.e. True or False. These values are not case-sensitive depending upon programming languages. The name Boolean comes from the branch of mathematics called Boolean algebra, named after George Bool the mathematician....

Difference Between Boolean and Other Data Types

...

Logical and Boolean Operators

...

1. Logical AND Operator ( && )

...

2. Logical OR Operator ( || )

...

3. Logical NOT Operator ( ! )

...

Relational and Boolean Operators

In programming languages, there are three types of data which are Booleans, Text, and Numbers. It is important to understand the differences between them and some basics about them....

Conclusion

In programming, boolean operators are logical operators(AND, OR, and NOT) that are symbols that allow you to combine or modify conditions to make logical evaluations. They are utilized to perform logical operations on boolean values (true or false). They are used to control the flow of a program....