Logical AND Operator
The '&&' symbol represents the logical AND operator in programming. This operator evaluates multiple operands in an expression, requiring all conditions to be true in order to return a value of true or 1. Otherwise, it will return false or 0.
Note: If the value of both is non-zero, the condition will remain true. Otherwise, the logical AND (&&) operator returns 0 (false).
Syntax
(condition1 && condition2)
There are a pair of requirements specified in the aforementioned syntax, referred to as condition1 and condition2, separated by the double (&&) ampersand symbol. When both conditions are satisfied, the logical AND operator yields a Boolean value of 1 or true. If either or both conditions are not met, it will result in a false outcome.
Truth table of the Logical AND (&&) operator
| A | B | A && B |
|---|---|---|
1 |
1 | 1 |
1 |
0 | 0 |
0 |
1 | 0 |
0 |
0 | 0 |
Example 1: Program to demonstrate the Logical AND Operator in C
#include <stdio.h>
#include <conio.h>
int main ()
{
// declare variable
int n = 20;
// use Logical AND (&&) operator to check the condition
printf (" %d \n", (n == 20 && n >= 8)); // condition is true, therefore it returns 1
printf (" %d \n", (n >= 1 && n >= 20));
printf (" %d \n", (n == 10 && n >= 0));
printf (" %d \n", (n >= 20 && n <= 40));
return 0;
}
Output
1
1
0
1
A sample code snippet that demonstrates how to identify the maximum number by employing the Logical AND operator is shown below:
num1 = 10
num2 = 20
if num1 > num2 and num1:
max_num = num1
else:
max_num = num2
print("The largest number is:", max_num)
#include <stdio.h>
#include <conio.h>
int main ()
{
// declare integer type variable
int x, y, z;
printf (" Enter the first number: ");
scanf ("%d", &x);
printf (" Enter the second number: ");
scanf ("%d", &y);
printf (" Enter the third number: ");
scanf ("%d", &z);
// use logical AND operator to validate the condition
if ( x >= y && x >= z )
{
printf (" %d is the largest number of all. ", x);
}
else if ( y >= x && y >= z)
{
printf (" %d is the largest number of all. ", y);
}
else
{
printf ( " %d is the largest number of all. ", z);
}
return 0;
}
Output
Enter the first number: 20
Enter the second number: 10
Enter the third number: 50
50 is the largest number of all
Example 3: Code snippet demonstrating the utilization of the Logical AND (&&) operator to verify if the individual is a teenager.
#include <stdio.h>
#include <conio.h>
int main ()
{
// declare variable
int age;
printf (" Enter the age: ");
scanf (" %d", &age); // get age
// use logical AND operator to check more than one condition
if ( age >= 13 && age <= 19)
{
printf (" %d is a teenager age. ", age);
}
else
{
printf (" %d is not a teenager age. ", age);
}
return 0;
}
Output
Enter the age: 17
17 is a teenager age.
2nd execution:
Enter the age: 10
10 is not a teenager age.
Example 4: Code to verify if the provided number falls within the specified range or not.
#include <stdio.h>
int main ()
{
int num;
printf (" Enter a number between 1 to 50: ");
scanf (" %d", &num); //get the number
// use logical AND operator to check condition
if ( (num > 0 ) && (num <= 50))
{
printf (" The entered number is in the range 0 and 50. ");
}
else if ( ( num > 50 ) && ( num <= 100))
{
printf (" The entered number is in the range 50 and 100. ");
}
else
{
printf (" Please enter the number is in the defined range. ");
}
return 0;
}
Output
Enter a number between 1 to 50: 19
The entered number is in the range 0 and 50.
2nd Run
Enter a number between 1 to 50:
51
The entered number is in the range 50 and 100.
3rd execution:
Enter a number between 1 to 50:
0
Please enter the number is in the defined range.
Example 5: A program designed to authenticate the accuracy of the username and password input by the user against the predefined username and password stored in the system.
#include <stdio.h>
#include <string.h>
// use #define macro to define the values for UserName and Password
#define UserName "system"
#define Password "admin@123"
int main ()
{
// declare character type array
char un[50], pass[50];
// take UserName and Password from user
printf ( " Enter the username: " );
scanf (" %s", un);
printf ( " Enter the password: " );
scanf (" %s", pass);
// use if statement and Logical AND operator to validate the condition
if (strcmp (UserName, un) == 0 && strcmp (Password, pass) == 0)
{
printf (" \n The user's credentials are correct. ");
}
else
{
printf ( " \n The user's credentials are incorrect. ");
}
return 0;
}
Output
Enter the username: system
Enter the password: admin@123
The user's credentials are correct.
2nd execution
Enter the username: system
Enter the password: admin@1234
The user's credentials are incorrect.