• Home
  • 3.10 Logical AND Operator (&&)

3.10 Logical AND Operator (&&)

View Categories

3.10 Logical AND Operator (&&)

< 1 min read

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 true only when both operands evaluate to true.
  • If either operand evaluates to false, the result is false.
  • (age >= 18) evaluates whether the age requirement is satisfied.
  • has_license indicates whether the person has a valid driving license.
  • The expression (age >= 18) && has_license evaluates to true only when both conditions are satisfied.
  • By default, std::cout displays Boolean values as 1 for true and 0 for false.
  • return 0; terminates the program successfully.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *