[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.1.6 Values as True and False

There is a notion in MOO of true and false values; every value is one or the other. The true values are as follows:

All other values are false:

There are four kinds of expressions and two kinds of statements that depend upon this classification of MOO values. In describing them, I sometimes refer to the truth value of a MOO value; this is just true or false, the category into which that MOO value is classified.

The conditional expression in MOO has the following form:

 
expression-1 ? expression-2 | expression-3

First, expression-1 is evaluated. If it returns a true value, then expression-2 is evaluated and whatever it returns is returned as the value of the conditional expression as a whole. If expression-1 returns a false value, then expression-3 is evaluated instead and its value is used as that of the conditional expression.

 
1 ? 2 | 3           =>  2
0 ? 2 | 3           =>  3
"foo" ? 17 | {#34}  =>  17

Note that only one of expression-2 and expression-3 is evaluated, never both.

To negate the truth value of a MOO value, use the `!' operator:

 
! expression

If the value of expression is true, `!' returns 0; otherwise, it returns 1:

 
! "foo"     =>  0
! (3 >= 4)  =>  1

The negation operator is usually read as "not."

It is frequently useful to test more than one condition to see if some or all of them are true. MOO provides two operators for this:

 
expression-1 && expression-2
expression-1 || expression-2

These operators are usually read as "and" and "or," respectively.

The `&&' operator first evaluates expression-1. If it returns a true value, then expression-2 is evaluated and its value becomes the value of the `&&' expression as a whole; otherwise, the value of expression-1 is used as the value of the `&&' expression. Note that expression-2 is only evaluated if expression-1 returns a true value. The `&&' expression is equivalent to the conditional expression

 
expression-1 ? expression-2 | expression-1

except that expression-1 is only evaluated once.

The `||' operator works similarly, except that expression-2 is evaluated only if expression-1 returns a false value. It is equivalent to the conditional expression

 
expression-1 ? expression-1 | expression-2

except that, as with `&&', expression-1 is only evaluated once.

These two operators behave very much like "and" and "or" in English:

 
1 && 1                  =>  1
0 && 1                  =>  0
0 && 0                  =>  0
1 || 1                  =>  1
0 || 1                  =>  1
0 || 0                  =>  0
17 <= 23  &&  23 <= 27  =>  1


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on September, 30 2008 using texi2html