Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

and — alternative spelling of logical AND

`and` is an alternative spelling of `&&` in C++. The built-in operator tests whether both conditions are true and skips the right operand when the left operand is false.

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

Syntax

lhs and rhs

and is an alternative token for &&, not a separate operation. Both spellings have the same semantics, precedence, and associativity; only their spelling differs. Use the syntax lhs and rhs.

For the built-in operator, both operands are contextually converted to bool, and the result has type bool. The result is true only when both operands are true. The operator groups left-to-right.

Evaluation short-circuits: the left operand is evaluated first; if it is false, the right operand is not evaluated. In the example, divisor != 0 is false, so 12 / divisor is not evaluated. The program should print false.

Do not confuse logical and (&&) with bitwise &: they are different operators, and & does not provide this skipping of the right operand. The description of conversions, result, and short-circuiting applies to the built-in operator.

Examples

#include <iostream>

int main() {
    const int divisor = 0;
    const bool matches = (divisor != 0) and (12 / divisor == 3);
    std::cout << std::boolalpha << matches << '\n';
}

Result

false

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