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!" ); } |
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.