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


Output

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.

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....