if, else Keywords

Keyword Index

Conditional statement.

Keyword if is used for conditional execution. The basic form of if uses the following syntax:

if (expression)
   statement1
Alternatively, if may be used together with else, using the following syntax:
if (expression)
   statement1
else
   statement2
If expression is nonzero when evaluated, then statement1 is executed. In the second case, statement2 is executed if the expression is 0.

An optional else can follow an if statement, but no statements can come between an if statement and an else. Of course, both statement1 and statement2 may be compound statements (i.e. a sequence of statements enclosed in braces). Here will be given some legal examples:
if (count < 50) count++;

if (x < y) z = x;
else z = y;

if (x < y)
  {
    printf ("x is smaller");
    return x;
  }
else
  {
    printf ("x is greater")
    return y;
  }
The #if and #else preprocessor statements look similar to the if and else statements, but have very different effects. They control which source file lines are compiled and which are ignored.