C - if statement introduction


The if keyword is used to make decisions based upon simple comparisons.

Here's the basic format:


The evaluation is a comparison, a mathematical operation, the result of a function or some other condition.

If the condition is true, the statements (or statement) enclosed in braces are executed; otherwise, they're skipped.

The if statement's evaluation need not be mathematical. It can be a function that returns a true or false value; for example:


Any non-zero value is considered true in C. Zero is considered false. So this statement is always true:


And this statement is always false:


When only one statement belongs to an if statement, the braces are optional.

Related Topic