Back to DFS's C Page


Single- and Double-Option IF Statements

If statements are used to select whether a sequence of statements should be executed or not. In this way, they control the flow of the program. If there is no choice to be made, each statement in a sequence of instructions is simply executed one after another. A real-life example of this sort of sequencing would be the following: 1) Get up; 2) Eat breakfast; 3) Go to school.

The Single-Option If Statement

However, sometimes you want to do something (or something is to be done) only if something else is happening or is true. A real-life example of this would be the following:

  1. Get up;
  2. If you have enough time, then eat breakfast;
  3. Go to school.

Many of us end up skipping breakfast because we don't have enough time. This kind of situation occurs frequently in programming.

The flowchart fragment illustrates Step 2. In this flowchart fragment, the testing of the condition is done in the diamond-shaped decision box. If the condition is found to be true, then the extra action, which is represented by the rectangular process box, is performed. (In the real-life example, this would be the eating of breakfast.) Otherwise, the extra action in the flowchart would simply be skipped.

In C, we use the following if-statement construct to conditionally do something:

if (expression)
    statement

The whole construct is considered to be a single C statement. If is a reserved word. The expression is a boolean expression which evaluates to either true or false. False has a value of zero and true is non-zero (anything else). Parens are used to enclose the expression inside the conditional statement. This statement is executed only if the condition is found to be true. An example of this structure in C would be:

if (num > 10)
   printf("Your number is too large.");

The statement can represent any C statement, e.g., a for statement or another if statement. The following example illustrates the use of a compound statement in this structure:

if (num < 0)
{
   num = 0;
   printf("The value was negative and has been reset to 0.");
}

The Double-Option If Statement

Sometimes, you want to do one thing if something is happening (or is true) and something else otherwise. A real-life example of this would be the following:

  1. Get up;
  2. If you have enough time,
       then eat breakfast
       otherwise grab something to take along;
  3. Go to school.

The flowchart fragments illustrate two ways of showing Step 2.

To deal with two mutually exclusive choices in C, we use the following if-statement construct:

if (expression) 
   statementA;
else
   statementB;

Again, the whole construct is considered to be a single C statement and the condition is a boolean expression which evaluates to either true or false. The statementA inside the if statement is executed only if the condition is found to be true. else is a reserved word which introduces statementB, which is executed only if the condition evaluates to false.

This kind of indentation reduces the number of levels, but obscures the fact that the whole conditional statement is a single statement in its own right. Thus, the placement of the else keyword directly below the if keyword incorrectly implies that it is on a par syntactically with if. However, there are two good reasons for this: (1) Since the if construct is itself a statement which can be used inside another if statement, indentation levels can become numerous. (2) It is easier to visualize the structure of the program, since an else is associated with the previous outstanding if.

An example of the double-option if-statement structure in C would be:

if (num > 10)
   printf("Your number is too large.\n");
else
   printf("Your number is OK.\n");

N.B. These embedded statements could each be statements of any type, e.g., a for loop, a compound statement, or even another if statement (but that is another story). Therefore, the single-option if consists of a minimum of two statements while the double-option if consists of at least three statements.


© 1997-99 DFStermole
HTMLified: 2 Oct 98
Modified 27 Dec 99