Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Logical AND operator (&&)

The built-in && operator produces true only when both conditions are true. It evaluates the left operand first and skips the right operand when the left is false; this is short-circuit evaluation.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

left && right

Built-in && contextually converts its operands to bool and returns a bool: true only when both are true. It groups left-to-right: a && b && c means (a && b) && c.

The left operand is evaluated first. If it is false, the right operand is not evaluated; otherwise, evaluation of the left is sequenced before evaluation of the right. Thus, the example's first expression skips positive(7), while the second calls it: checks is 1.

Do not confuse && with bitwise &, which does not provide short-circuiting. and is an alternative spelling of &&. An overloaded operator&& is a function call and does not provide short-circuiting: both operands are evaluated. Operator notation still preserves left-before-right sequencing; the result type depends on the overload.

This entry targets C++23. The supplied source is an evolving draft, not a frozen C++23 publication. You can check the example locally with -std=c++23. The displayed code was run unchanged in an isolated environment, and its standard output matched the stated output exactly. This verification does not attest to operation in a production runtime.

Examples

#include <iostream>

int main() {
    int checks = 0;
    auto positive = [&checks](int value) {
        ++checks;
        return value > 0;
    };

    bool first = false && positive(7);
    bool second = true && positive(7);

    std::cout << std::boolalpha;
    std::cout << first << ' ' << second << ' ' << checks << '\n';
}

Result

false true 1

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources