| |
The if-else Statements | page 9 of 15 |
The general syntax of the if -else statement is as follows:
if (expression)
statement1;
else
statement2;
if statements may omit the else option if it results in one-way selection.
if (expression)
statement1;
If the expression is non-zero, statement is executed, otherwise nothing is executed. The following flowchart illustrates the flow of control.

The full if -else statement allows for two-way control. If the value of the expression is true, statement1 is executed. If the value of the expression equals false, the else option results in statement2 being executed. The following flowchart from handout, H.A.8.2, illustrates the flow of control.

The expression being tested must always be placed in parentheses. This is a common source of syntax errors.
|