Previous Table of Contents Next


Relational Precedence

Relational operators and logical operators, being C++ expressions, each return a value: 1 (true) or 0 (false). Like all expressions, they have a precedence order that determines which relations are evaluated first. This fact is important when determining the value of the statement

if ( x > 5 && y > 5 || z > 5)

It might be that the programmer wanted this expression to evaluate true if both x and y are greater than 5 or if z is greater than 5. On the other hand, the programmer might have wanted this expression to evaluate true only if x is greater than 5 and if it is also true that either y is greater than 5 or z is greater than 5.

If x is 3, and y and z are both 10, the first interpretation will be true (z is greater than 5, so ignore x and y), but the second will be false. (It is not true that x is greater than 5, so it does not matter that y or z are greater than 5.)

Although precedence will determine which relation is evaluated first, parentheses can both change the order and make the statement clearer:

if ( (x > 5) && (y > 5 || z > 5) )

Using the values given earlier, this statement is false. Because it is not true that x is greater than 5, the left side of the AND statement fails, and thus the entire statement is false. Remember that an AND statement requires that both sides be true—something isn’t both “good tasting” AND “good for you” if it isn’t good tasting.

It is often a good idea to use extra parentheses to clarify what you want to group. Remember, the goal is to write programs that work and that are easy to read and understand.

More About Truth and Falsehood

In C++ zero is false, and any other value is true. Because expressions always have a value, many C++ programmers take advantage of this feature in their if statements. A statement such as

if (x)  // if x is true (nonzero)
 x = 0;

can be read as “If x has a nonzero value, set it to 0.” This is a bit of a cheat; it would be clearer if written

if (x != 0) // if x is nonzero
 x = 0;

Both statements are legal, but the latter is clearer. It is good programming practice to reserve the former method for true tests of logic, rather than for testing for nonzero values.

These two statements are also equivalent:

if (!x)  // if x is false (zero)
if (x == 0) // if x is zero

The second statement, however, is somewhat easier to understand and is more explicit.

Summary

In this hour you learned what a statement is. You also learned that an expression is any statement that returns a value. You learned that whitespace can be used to make your program more readable, and you saw how to use the mathematical and assignment operators.

You also learned how to use the prefix and postfix operators, as well as the relational operators. You learned how to use the if statement, how to use else, and how to create complex and nested if statements.

Q&A

Q Why use unnecessary parentheses when precedence will determine which operators are acted on first?
A Although it is true that the compiler will know the precedence and that a programmer can look up the precedence order, code that is easy to understand is easier to maintain.
Q If the relational operators always return 1 or 0, why are other values considered true?
A The relational operators return 1 or 0, but every expression returns a value, and those values can also be evaluated in an if statement. Here’s an example:
if ( (x = a + b) == 35 )

This is a perfectly legal C++ statement. It evaluates to a value even if the sum of a and b is not equal to 35. Also note that x is assigned the value that is the sum of a and b in any case.
Q What effect do tabs, spaces, and new lines have on the program?
A Tabs, spaces, and new lines (known as whitespace) have no effect on the program, although whitespace can make the program easier to read.
Q Are negative numbers true or false?
A All nonzero numbers, positive and negative, are true.


Previous Table of Contents Next