# Singular

### 6.3.2 Evaluation of logical expressions

All arguments of a logical expression are first evaluated and then the value of the logical expression is determined. For example, the logical expressions `(a || b)` is evaluated by first evaluating `a` and `b`, even though the value of `b` has no influence on the value of `(a || b)`, if `a` evaluates to true.

Note, that this evaluation is different from the left-to-right, conditional evaluation of logical expressions (as found in most programming languages). For example, in these other languages, the value of `(1 || b)` is determined without ever evaluating `b`. This causes some problems with boolean tests on variables, which might not be defined at evaluation time. For example, the following results in an error, if the variable `i` is undefined:

 ```if (defined(i) && i > 0) {} // WRONG!!! ```

This must be written instead as:

 ```if (defined(i)) { if (i > 0) {} } ```

However, there are several short work-arounds for this problem:

1. If a variable (say, `i`) is only to be used as a boolean flag, then define (value is TRUE) and undefine (value is FALSE) `i` instead of assigning a value. Using this scheme, it is sufficient to simply write

 ```if (defined(i)) ```

in order to check whether `i` is TRUE. Use the command `kill` to undefine a variable, i.e. to assign it a FALSE value (see kill).

2. If a variable can have more than two values, then define it, if necessary, before it is used for the first time. For example, if the following is used within a procedure

 ```if (! defined(DEBUG)) { int DEBUG = 1;} ... if (DEBUG == 3) {...} if (DEBUG == 2) {...} ... ```

then a user of this procedure does not need to care about the existence of the `DEBUG` variable -- this remains hidden from the user. However, if `DEBUG` exists globally, then its local default value is overwritten by its global one.

### Misc 