Logical Operators

Logical operators go hand-in-hand with relational ones. They are logical and (&&), logical or (||), logical not (!). The first two are binary, and the last one unary. These operators let you combine the results of several variable tests into one result. 'Logical and' means both sides must be true, 'logical or' means at least one side must be true. 'Logical not' gives back the reverse of the true/false state of the value tested.

a && b;     a || b;    !a;    ( a<=b ) && ( c > d );    !a && b || c;

Sometimes logical expressions can get quite complicated. It is best to use parentheses wherever possible, both to aid the reader's understanding and to avoid obscure bugs.

© 1997 by Wrox Press. All rights reserved.