HP C A.06.05 Reference Manual
Expressions and Operators
Logical Operators (&&, ||, !)
Chapter 5 113
Logical Operators (&&, ||, !)
Syntax
exp1
&&
exp2
Logical AND.
exp1
||
exp2
Logical OR.
!exp1
Logical NOT.
Arguments
exp1
Any expression.
exp2
Any expression.
Description
The logical AND operator (&&) and the logical OR (||) operator evaluate the truth or falsehood
of pairs of expressions. The AND operator evaluates to 1 if and only if both expressions are
true. The OR operator evaluates to 1 if either expression is true. To test whether y is greater
than x and less than z, you would write
(x < y) && (y < z)
The logical negation operator (!) takes only one operand. If the operand is true, the result is
false; if the operand is false, the result is true.
The operands to the logical operators may be integers or floating-point objects. The expression
1 && -5
results in 1 because both operands are nonzero. The same is true of the expression
0.5 && -5
Logical operators (and the comma and conditional operators) are the only operators for which
the order of evaluation of the operands is defined. The compiler must evaluate operands from
left to right. Moreover, the compiler is guaranteed not to evaluate an operand if it is
unnecessary. For example, in the expression
if ((a != 0) && (b/a == 6.0))
if a equals 0, the expression (b/a == 6) will not be evaluated. This rule can have unexpected
consequences when one of the expressions contains side effects.