Expressing Good Condition

We took a quick look at some of the operators that we can use in conditional expressions in the previous part. Here we're going to look a little more carefully at the expression that goes into the if.

The first thing to notice is that in our general form of if statements:

if( expression )

we didn't distinguish the expression as begin any particular kind of expression. This means that an expression such as:

if( a + b )

is perfectly legal. The natural question is "what does it do?" The answer is simple, but obscure. In C, any expression with the value 0 is false and any non-zero value is true. So if the sum of a and b is 0 then the if would jump down to the else part (if there is one). On the other hand, if a + b is not 0, then we'll do the code in the true branch of the if. Now all of this is to explain what happens if you do something like that, not to suggest that it's a good thing to do. While experienced programmers will occasionally use idioms that include such expressions, it's not the usual course of action and it is not recommended for this stage in your programming.

One particularly common and related mistake is to try to test for equality with something like:

if( a = 10 )

It turns out that the assignment operator (=) is a perfectly valid expression operator. So the expression has the effect of assigning 10 to a. Then if the value assigned was non-zero (as is 10), the if takes the true branch. We should test as in:

if( a == 10 )

and use the comparison operator (==). One suggestion to help prevent (or at least catch) such mistakes is that when comparing a variable to a constant or to an expression more involved than a single variable, to put the variable on the right of the comparison. So, for example, if we make the mistake:

if( 10 = a )

the compiler will complain because we can't assign to 10.

We can similarly have a statement like:

x = a < b ;

and we'd naturally ask what gets assigned to x. Here the operator < yields a value of 1 if the expression is true (that is if a is less than b) and 0 otherwise. So, x will be set to either 1 or 0 depending on whether a is less than b or not. (One note of caution: before the ANSI C standard, the exact value of true in this context was implementation dependent. So it's possible that a non-ANSI compiler might use some other value for true. The upshot is that you should avoid depending explicitly on x being 1 if it's true; just look to see if it's 0 or not.)

Combining Conditions

It's common for us to ask a compound condition like is a between 1 and 10. In math, we'd write 1 <= a <= 10. C doesn't allow such syntax. Instead we need to think of this as is a greater than or equal to 1 AND is a less than or equal to 10. So we'd express such a thing in C as

if( 1 <= a && a <= 10 )

Here the && means AND || means OR. To indicate a NOT, we use the exclamation point (!). We'll see more about these operators in the next part.

Write a C expression to test to see if the variable y is outside the range 1 to 100. (This range in inclusive so y must not be equal to 1 or to 100 or anything in between.)