next up previous contents
Next: Decisions Up: Exploring the Basics Previous: Simple Output

Boolean Expressions

An expression which evaluates to either true or false is called a boolean expression. Boolean expressions are used extensively in programming language constructs such as if-then-fi commands and while loops. Darwin offers and, or and not for building such expressions. The and returns true if and only if both arguments are true. The not operator negates a boolean value.

> u := true:   v := false:
> (u and v);
false
> (u and (not v));
true
> flag := true:
> not((not flag) = (not not not flag));
false
The or operator returns false if and only if both arguments are false.
> u := true:   v := false:
> (u or v);                                 # one must be true for `or'
> ((not u) or v);                           # u -> v
We can build more complex boolean expressions using these operators, expressions and the comparison operators. Table [*] contains a list of all comparison and boolean operators.

> (6 = 1 + 2 + 3) and (720 = factorial(6));
true
> x := 57:
> (mod(x, 2) = 0) or (mod(x, 3) = 0) or (mod(x, 5) = 0);  # is x divisible 
                                                          #  by 2, 3, or 5?
false
> 5 < 7;
true
> floor(6.5) <= 6;
true
> 1 <> 2;
true



Table: List of all comparison and boolean operators..
1.1 
Table: List of all comparison and boolean operators..
Operator Description
and true iff both operands are true
or false iff both operands are false
not true iff operand is false
= true iff operands are equal
< true iff left operand is less than right
> true iff right operand is greater than right
<> true iff operands are not equal
<= true iff left is less than or equal to right
>= true iff left is greater than or equal to right
 




next up previous contents
Next: Decisions Up: Exploring the Basics Previous: Simple Output
Gaston Gonnet
1998-09-15