HPlogo HP C/HP-UX Reference Manual: Version A.05.55.02 > Chapter 5 Expressions and Operators

Logical Operators (&&, ||, !)

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

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.

Truth Table for C's Logical Operators

In C, true is equivalent to any nonzero value, and false is equivalent to 0. The following table shows the logical tables for each operator, along with the numerical equivalent. All of the operators return 1 for true and 0 for false.

Table 5-10 Truth Table for C's Logical Operators

Operand

Operator

Operand

Result

zero

&&

zero

0

nonzero

&&

zero

0

zero

&&

nonzero

0

nonzero

&&

nonzero

1

zero

||

zero

0

nonzero

||

zero

1

zero

||

nonzero

1

nonzero

||

nonzero

1

not applicable

!

zero

1

!

nonzero

0

 

Examples of Expressions Using the Logical Operators

The following table shows a number of examples that use relational and logical operators. The logical NOT operator has a higher precedence than the others. The AND operator has higher precedence than the OR operator. Both the logical AND and OR operators have lower precedence than the relational and arithmetic operators.

Table 5-11 Examples of Expressions Using the Logical Operators

Given the following declarations:

int j = 0, m = 1, n = -1;
float x = 2.5, y = 0.0;

Expression

Equivalent Expression

Result

j && m

(j) && (m)

0

j < m && n < m

(j < m) && (n < m)

1

m + n || ! j

(m + n) || (!j)

1

x * 5 && 5 || m / n

((x * 5) && 5) || (m / n)

1

j <= 10 && x >= 1 && m

((j <= 10) && (x >= 1)) && m

1

!x || !n || m+n((!x) || (!n)) || (m+n)0

x * y < j + m || n

((x * y) < (j + m)) || n

1

(x > y) + !j || n++

((x > y) + (!j)) || (n++)

1

(j || m) + (x || ++n)

(j || m) + (x || (++n))

2

 

Side Effects in Logical Expressions

Logical operators (and the conditional and comma operators) are the only operators for which the order of evaluation of the operands is defined. For these operators, operands must be evaluated from left to right. However, the system evaluates only as much of a logical expression as it needs to determine the result. In many cases, this means that the system does not need to evaluate the entire expression. For instance, consider the following expression:

if ((a < b) && (c == d))

The system begins by evaluating (a < b). If a is not less than b, the system knows that the entire expression is false, so it will not evaluate (c == d). This can cause problems if some of the expressions contain side effects:

if ((a < b) && (c == d++))

In this case, d is only incremented when a is less than b. This may or may not be what the programmer intended. In general, you should avoid using side effect operators in logical expressions.

Example

/* Program name is "logical_ops_example". This program */
/* shows how logical operators are used. */
#include <stdio.h>

int main(void)
{
int won_lottery, enough_vacation, money_saved;
char answer;

won_lottery = enough_vacation = money_saved = 0;

printf("\nThis program determines whether you can ");
printf("take your next vacation in Europe.\n");
printf("Have you won the lottery? y or n: ");
fflush(stdin);
scanf("%c", &answer);
if (answer == 'y')
won_lottery = 1;

printf("Do you have enough vacation days saved? \
y or n: ");
fflush(stdin);
scanf ("%c", &answer);
if (answer == 'y')
enough_vacation = 1;

printf("Have you saved enough money for the trip? \
y or n: ");
fflush(stdin);
scanf("%c", &answer);
if (answer == 'y')
money_saved = 1;

printf("\n");
if (won_lottery)
{
printf("Why do you need a program to decide if you");
printf(" can afford a trip to Europe?\n");
} /* end if */
if (won_lottery || (enough_vacation &&money_saved))
printf("Look out Paris!\n");
else if (enough_vacation &&(!money_saved))
printf("You've got the time, but you haven't got \
the dollars.\n");
else if (!enough_vacation || (!money_saved))
{
printf("Tough luck. Try saving your money and ");
printf("vacation days next year.\n");
} /* end else/if */
}

If you execute this program, you get the following output:

This program determines whether you can take your next vacation
in Europe.
Have you won the lottery? y or n: y
Do you have enough vacation days saved? y or n: n
Have you saved enough money for the trip? y or n: n

Why do you need a program to decide if you can afford a trip to
Europe?
Look out Paris!

© Hewlett-Packard Development Company, L.P.