Table of Contents
The logical AND operator (&&) combines two Boolean expressions and returns true only if both expressions evaluate to true. If either expression evaluates to false, the result of the entire expression is false.
The logical AND operator is commonly used when multiple conditions must be satisfied before a statement is executed.
Source Code #
#include <iostream>
int main()
{
int age = 22;
bool has_license = true;
bool can_drive = (age >= 18) && has_license;
std::cout << "Can drive: " << can_drive << '\n';
return 0;
}
Output #
Can drive: 1
Explanation #
- The logical AND operator (
&&) combines two Boolean expressions. - It returns
trueonly when both operands evaluate totrue. - If either operand evaluates to
false, the result isfalse. (age >= 18)evaluates whether the age requirement is satisfied.has_licenseindicates whether the person has a valid driving license.- The expression
(age >= 18) && has_licenseevaluates totrueonly when both conditions are satisfied. - By default,
std::coutdisplays Boolean values as1fortrueand0forfalse. return 0;terminates the program successfully.