Logical AND Operator ( && )
The logical AND operator (&&) is a binary operator that returns true only if both of its operands are true. Otherwise, if one of the operands is false then it returns false. Truth table for the AND operator is given below:
Operand 1 |
Operand 2 |
Result |
---|---|---|
true |
true |
true |
true |
false |
false |
false |
true |
false |
false |
false |
false |
Syntax of Logical AND
expression1 && expression2
Example of Logical AND
Below is the implementation of the above method:
C++
// C++ Program to illustrate the logical AND Operator #include <iostream> using namespace std; int main() { // initialize variables int age = 25; bool isStudent = true ; // Using AND operator in if condition if (age > 18 && isStudent) { cout << "You are eligible for a student discount." << endl; } else { cout << "You are not eligible for a student " "discount." << endl; } return 0; } |
C
#include <stdio.h> #include <stdbool.h> int main() { // initialize variables int age = 45; bool isStudent = false ; // Using AND operator in if condition if (age > 18 && isStudent) { printf ( "You are eligible for a student discount.\n" ); } else { printf ( "You are not eligible for a student discount" ); } return 0; } |
Java
/*package whatever //do not write package name here */ import java.io.*; class GFG { public static void main (String[] args) { // initialize variables int age = 23 ; boolean isStudent = true ; // Using AND operator in if condition if (age > 18 && isStudent) { System.out.println( "You are eligible for a student discount.\n" ); } else { System.out.println( "You are not eligible for a student discount" ); } } } |
Python3
# initialize variables age = 23 isStudent = True # Using AND operator in if condition if age > 18 and isStudent: print ( "You are eligible for a student discount." ) else : print ( "You are not eligible for a student discount" ) |
Javascript
// initialize variables let age = 23; let isStudent = true ; // Using AND operator in if condition if (age > 18 && isStudent) { console.log( "You are eligible for a student discount.\n" ); } else { console.log( "You are not eligible for a student discount" ); } |
You are eligible for a student discount.
Explaination: In the code, we have used AND operator to check whether a person is eligible for a discount or not. So, we check if person’s age is greater than 18 and the person is a student. If a person’s age is greater then 18 and also a student the condition became true, the message “You are eligible for a student discount.” will be printed. Otherwise, the else statement is executed.
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.