Boolean Operations


The Boolean Operators

In addition to the comparison operators that produce boolean results (usually comparing numeric values), there are three boolean operators that take boolean values as operands and produce boolean results.

OperatorMeaning
&& AND: both operands must be true for the whole expression to be true
|| OR: one or the other operand must be true for the whole expression to be true (vertical bars are usually above the \ symbol on the keyboard)
! NOT (negation): if the operand is true then the expression is false; if the operand is false then the expression is true.

Examples:

    if ( ( 0 < i ) && ( i < k ) )   {  ... }
    if ( fish.atWall() || ( randNum < 2 ) )  { ... }
    if ( ( hour > 6 ) && ( ! todayIsSunday ) )  { ... }

Alyce Brady, Kalamazoo College