if
.
The first thing to notice is that in our general form of
if
statements:
if( expression )
if( a + b )
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 )
=
) 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 )
==
).
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 )
We can similarly have a statement like:
x = a < b ;
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.)
if( 1 <= a && a <= 10 )
&&
means AND ||
means OR.
To indicate a NOT, we use the exclamation point (!).
We'll see more about these operators in the
next part.
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.)