t3x.org / t3x / t3x-manual / 267.html
 
T3X - A Minimum Procedural Language
Version 8.1.5, Online Edition
Copyright(C) 1996-2004
Nils M Holm
 
Previous:
2.6.6 Relational Operators
TOC | Index | Back Next:
2.6.8 Conditional Expressions

2.6.7 Conjunctions and Disjunctions

The operators A/\B and A\/B reflect logical conjunction (AND) and disjunction (OR). Generally, the expression

A /\ B

evaluates to some true value, if and only if both A AND B evaluate to 'truth' and

A \/ B

yields a true result if either A OR B (or both of them) evaluate to `truth'.

More specifically, /\ and \/ are so-called short circuit operators. Since the expression A/\B can only lead to a true result, if all of its its operands are true, there is no actual need to evaluate B, if A already has evaluated to 'false'. Therefore, the second operand of a conjunction never will be evaluated by a T3X program, if the first one already is false. The result will be zero in this case. If, on the other hand, the first value is true, the result of the entire conjunctional expression will be the value of the second operand. Therefore, the result of

A /\ B

can be specified more precisely as

zero, if A=0 and
B, if A\=0.

Similarly, the expression A\/B can never become `false', if A already has been found out to be true. Therefore, no T3X program will ever evaluate B in such a case, and the result of the disjunction

A \/ B

can be explained more precisely as

A, if A\=0.
B, if A=0.

Like in ordinary algebra, conjunction binds stronger than disjunction:

A /\ B \/ C /\ D

equals

(A/\B) \/ (C/\D)

In chains of equal logical operations, the order of evaluation is from the left to the right (as in all binary operations). This means that chains of conjunctions will be evaluated up to the first `false' occurrence and chains of disjunctions will be processed up to the first `true' occurrence. In either case, the result of the entire chain is the value of the operand most recently processed.

There exists a connection between the logical operators and conditional statements: Because of their short circuit nature, logical operators may be used to implement flow control inside of expressions. The expression

A /\ B()

has almost the same meaning as

IF (A) B();

The only difference is that the expression yields a value, while the statement only has a side effect. Likewise, the expression

A \/ B()

has the same meaning as

IF (\A) B();

when ignoring the value of the expression. The IF statement will be explained in a later section.

Previous:
2.6.6 Relational Operators
TOC | Index | Back Next:
2.6.8 Conditional Expressions