Comparisons? Why that?

Comparisons are the most important things in programs. You (that means, your program) can't live without them.

Hard to do, isn't it?

No, not at all. That is the syntax:

if (expression)
 it_is_true;
else
 it_is_not_true;

Mind the semicolons!

it_is_true and it_is_not_true are two commands, not more or less. Of course, you can leave out else and it_is_not_true; if you have no purpose for them.
expression is an mathematic equation. If the result is not zero, it_is_true will be executed, else it_is_not_true.
The following operators can be used for comparisons: (they will return the right number, zero or not zero)

==equal
<less than
>greater than
<=less than or equal
>=greater than or equal
!=not equal
&&and
||or

IMPORTANT: NOT ONE '=' BUT TWO!!!!!
You won't get an error message if you write only '=' instead, but you program won't work!! (In fact, it's an legal expression, but more on that later)

Example:

if (a==45)
 printf("a is 45.");
else
 printf("a is not 45.");

(I know that that isn't meaningful... :-)

If you want more commands in comparisons, you must write them into { and }'s. Example:

if (a!=99)
 {
  printf("a is not 99.");
  e=77;
 }

(Note that you must not add a semicolon after the })


OK, that's it for now, I'm working on it... :-)

Chapter 4
Contents
Starting page
1