<<< Increment and Decrement Operators     Index     Logical operators short-circuiting >>>

Logical AND, logical OR operators

  • The logical AND && operator returns 1 if both exp1 and exp2 are non-zero:


        if ( exp1 && exp2 ) {
            //...
        }

#include <cassert>
void main()
{
    int x = 1;
    int y = 2;
    // true: y > x
    // true: x > 0
    if ( y > x && x > 0 ) {
        assert( y > x );
        assert( x > 0 );
    }
}
  • The logical OR || operator returns 1 if either exp1, or exp2, or both are non-zero:


        if ( exp1 || exp2 ) {
            //...
        }

void main()
{
    int x = 1;
    int y = 2;
    if ( x == 1 || y == 2 ) {
        //...
    }
}
<<< Increment and Decrement Operators     Index     Logical operators short-circuiting >>>