20.3 Operations for Booleans

The following boolean operations are only applicable to true and false.

  • bool1 or bool2

    The logical operator or evaluates to true if at least one of the two boolean operands bool1 and bool2 is true and to false otherwise.

    or first evaluates bool1. If the value is neither true nor false an error is signalled. If the value is true, then or returns true without evaluating bool2. If the value is false, then or evaluates bool2. Again, if the value is neither true nor false an error is signalled. Otherwise or returns the value of bool2. This short-circuited evaluation is important if the value of bool1 is true and evaluation of bool2 would take much time or cause an error.

    or is associative, i.e., it is allowed to write b1 or b2 or b3, which is interpreted as (b1 or b2) or b3. or has the lowest precedence of the logical operators. All logical operators have lower precedence than the comparison operators =, <, in, etc.

    gap> true or false;
    true
    gap> false or false;
    false
    gap> i := -1;; l := [1,2,3];;
    gap> if i <= 0 or l[i] = false then Print("aha\n"); fi;
    aha  # no error, because `l[i]' is not evaluated
    

  • bool1 and bool2

    The logical operator and evaluates to true if both boolean operands bool1 and bool2 are true and to false otherwise.

    and first evaluates bool1. If the value is neither true nor false an error is signalled. If the value is false, then and returns false without evaluating bool2. If the value is true, then and evaluates bool2. Again, if the value is neither true nor false an error is signalled. Otherwise and returns the value of bool2. This short-circuited evaluation is important if the value of bool1 is false and evaluation of bool2 would take much time or cause an error.

    and is associative, i.e., it is allowed to write b1 and b2 and b3, which is interpreted as (b1 and b2) and b3. and has higher precedence than the logical or operator, but lower than the unary logical not operator. All logical operators have lower precedence than the comparison operators =, <, in, etc.

    gap> true and false;
    false
    gap> true and true;
    true
    gap> false and 17;
    false  # is no error, because `17' is never looked at
    

  • fil1 and fil2

    and can also be applied to filters. It returns a filter that tests fil1(x) and fil2(x)

    gap> andfilt:= IsPosRat and IsInt;;
    gap> andfilt( 17 );  andfilt( 1/2 );
    true
    false
    

  • not bool

    The logical operator not returns true if the boolean value bool is false and true otherwise. An error is signalled if bool does not evaluate to true or false.

    not has higher precedence than the other logical operators, or and and. All logical operators have lower precedence than the comparison operators =, <, in, etc.

    gap> true and false;
    false
    gap> not true;
    false
    gap> not false;
    true
    

    [Top] [Previous] [Up] [Index]

    GAP 4 manual
    February 2000