An `if` statement like

if(x > max) max = x;is perhaps deceptively simple. Conceptually, we say that it checks whether the condition

As far as C is concerned,
a true/false condition can be represented as an integer.
(An integer can represent many values;
here we care about only two values:
``true'' and ``false.''
The study of mathematics involving only two values is called
Boolean algebra, after George Boole, a mathematician who
refined this study.)
In C, ``false'' is represented by a value of 0 (zero),
and ``true'' is represented by any value that is nonzero.
Since there are many nonzero values (at least 65,534, for
values of type `int`),
when we have to pick a specific value for ``true,''
we'll pick 1.

The relational operators such as
`<`, `<=`, `>`, and `>=`
are in fact operators, just like `+`, `-`, `*`,
and `/`.
The relational operators take two values, look at them, and
``return'' a value of 1 or 0 depending on whether the
tested relation was true or false.
The complete set of relational operators in C is:

<less than<=less than or equal>greater than>=greater than or equal==equal!=not equal

For example,

We've now encountered perhaps the most easy-to-stumble-on
``gotcha!'' in C:
the equality-testing operator is `==`,
not a single `=`, which is assignment.
If you accidentally write

if(a = 0)(and you probably will at some point; everybody makes this mistake), it will

The relational operators work with arbitrary numbers
and generate true/false values.
You can
also
combine true/false values by using the
Boolean operators,
which take true/false values as operands
and compute new true/false values.
The three Boolean operators are:

&&and||or!not (takes one operand; ``unary'')

The

For example, to test whether the variable `i` lies
between 1 and 10, you might use

if(1 < i && i < 10) ...Here we're expressing the relation ``

It's important to understand why the more obvious expression

if(1 < i < 10) /* WRONG */would not work. The expression

Relational and Boolean expressions are usually used
in contexts such as an `if` statement, where something is
to be done or not done depending on some condition.
In these cases
what's actually checked is whether the expression representing
the condition has a zero or nonzero value.
As long as the expression is a relational or Boolean expression,
the interpretation is just what we want.
For example,
when we wrote

if(x > max)the

But what if the expression is not a relational or Boolean expression?
As far as C is concerned,
the controlling expression
(of conditional statements like `if`)
can
in fact
be *any* expression:
it doesn't have to ``look like'' a Boolean expression;
it doesn't have to contain relational or logical operators.
All C looks at
(when it's evaluating an `if` statement,
or anywhere else where it needs a true/false value)
is whether the expression evaluates to 0 or nonzero.
For example,
if you have
a variable `x`,
and you want to do something if `x` is nonzero,
it's possible to write

if(x)and the statement will be executed ifstatement

This possibility
(that the controlling expression of an `if` statement
doesn't have to ``look like'' a Boolean expression)
is both useful and potentially confusing.
It's useful when you have a variable or a function that is
``conceptually Boolean,''
that is,
one
that you consider to hold
a true or false (actually nonzero or zero) value.
For example, if you have a variable `verbose`
which contains a nonzero value when your program should run in
verbose mode and zero when it should be quiet,
you can write things like

if(verbose) printf("Starting first pass\n");and this code is both legal and readable, besides which it does what you want. The standard library contains a function

if(isupper(c)) ...Both of these examples (

However, you will eventually come across code like

if(n) average = sum / n;where

``Coding shortcuts'' like these can seem cryptic, but they're also quite common, so you'll need to be able to recognize them even if you don't choose to write them in your own code. Whenever you see code like

if(x)or

if(f())where

Read sequentially: prev next up top

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