< previous page page_53 next page >

Page 53
if Statement
Form 1
        if (expression)
          statement;
        next statement;

If the expression is evaluated as true, the statement is executed and the program continues with the next statement. If the expression is not true, the statement is ignored and the program jumps to the next statement.
Remember that the statement can be a single statement ending with a semicolon or a compound statement enclosed in braces.
Advanced if Statements
It is worth noting that any statement can be used in an if or else clause, even another if or else statement. Thus, you might see complex if statements in the following form:
if (expression1)
{
    if (expression2)
       statement1;
    else
    {
       if (expression3)
          statement2;
       else
          statement3;
    }
}
else
    statement4;
This cumbersome if statement says, If expression1 is true and expression2 is true, execute statement1. If expression1 is true but expression2 is not true, then if expression3 is true, execute statement2. If expression1 is true but expression2 and expression3 are false, execute statement3. Finally, if expression1 is not true, execute statement4. As you can see, complex if statements can be confusing!
Listing 4.4 gives an example of such a complex if statement.

 
< previous page page_53 next page >

If you like this book, buy it!