section 2.6: Relational and Logical Operators

If it isn't obvious, >= is greater-than-or-equal-to, <= is less-than-or-equal-to, == is equal-to, and != is not-equal-to. We use >=, <=, and != because the symbols >=, <=, and != are not common on computer keyboards, and we use == because equality testing and assignment are two completely different operations, but = is already taken for assignment. (Obviously, typing = when you mean == is a very easy mistake to make, so watch for it. Some compilers will warn you when you use one but seem to want the other.)

The fact that evaluation of the logical operators && and || ``stops as soon as the truth or falsehood of the result is known'' refers to the fact that

``false'' AND anything is false
or, in C,
(0 && anything) == 0
while, on the other hand,
``true'' OR anything is true
or, in C,
(1 || anything) == 1
Looking at these another way, if you want to do something if thing1 is true and thing2 is true, and you've just noticed that thing1 is false, you don't even need to check thing2. Similarly, if you're supposed to do something if thing3 is true or thing4 is true, and you notice that thing3 is true, you can go ahead and do whatever it is you're supposed to do without checking thing4.

C works the same way, and if it's not true that ``most C programs rely on these properties,'' it's certainly true that many do.

For another example of the usefulness of this ``short-circuiting'' behavior, suppose we're taking the average of n numbers. If n is zero, that is, if we don't have any numbers to take the average of, we don't want to divide by zero. Code like

	if(n != 0 && sum / n > 1)
is common: it tests whether n is nonzero and the average is greater than 1, but it does not have to worry about dividing by zero. (If, on the other hand, the compiler always evaluated both sides of the && before checking to see whether they were both true, the code above could divide by zero.)

page 42

Note the extra parentheses in

	(c = getchar()) != '\n'
Since this is a common idiom, you'll need to remember the parentheses. What would
	c = getchar() != '\n'
do?

C's treatment of Boolean values (that is, those where we only care whether they're true or false) is straightforward. We'll have more to say about it later, but for now, note that a value of zero is ``false,'' and any nonzero value is ``true.'' You might also note that there is no necessary connection between statements like if() which expect a true/false value and operators like >= and && which generate true/false values. You can use operators like >= and && in any expression, and you can use any expression in an if() statement.

The authors make a good point about style: if valid is conceptually a Boolean variable (that is, it's an integer, but we only care about whether it's zero or nonzero, in other words, ``false'' or ``true''), then

	if(valid)
is a perfectly reasonable and readable condition. However, when values are not conceptually Boolean, I encourage you to make explicit comparisons against 0. For example, we could have expressed our average-taking code as
	if(n && sum / n > 1)
but I think it's clearer to be explicit and say
	if(n != 0 && sum / n > 1)
(However, many C programmers feel that expressions like
	if(n && sum / n > 1)
are ``more concise,'' so you will see them all the time and you should be able to read them.)


Read sequentially: prev next up top

This page by Steve Summit // Copyright 1995, 1996 // mail feedback