Previous: 2.5.3 More on Expressions
Up: 2.5 More C Statements
Next: 2.5.5 Controlling Loop Termination
Previous Page: 2.5.3 More on Expressions

A Simple Loop --- while

Our latest program, pay2.c, still calculates pay for only one individual. If we have 50 people on the payroll, we must run the above program separately for each person. For our program to be useful and flexible, we should be able to repeat the same logical process of computation as many times as desired; i.e. it should be possible to write a program that calculates pay for any number of people.

Task

PAY3: Same as PAY2, except that the program reads data, computes pay, and prints the data and the pay for a known number of people.

Let us first see how to repeat the process of reading data, calculating pay, and printing the results a fixed number, say 10, times. To repeatedly execute an identical group of statements, we use what is called a loop. To count the number of times we repeat the computation, we use an integer variable, count. The logic we wish to implement is:

set count to 0
     repeat the following as long as count is less than 10
          read data
          calculate pay
          print results
          increase count by 1
Initially, we set count to zero and we will repeat the process as long as count is less than 10. Each time we execute the loop, we increment count so that for each value of count (0, 1, 2, ..., 9), one set of data is processed. When count is 10, i.e. it is NOT less than 10, the repeating or looping is terminated.

The C language provides such a control construct; a while statement is used to repeat a statement or a block of statements. The syntax for a while statement is:

The keyword while and the parentheses are required as shown. The <expression> is a condition as it was for the if statement, and the <statement> may be any statement in C such as an empty statement, a simple statement, or a compound statement (including another while statement).

The semantics of the while statement is as follows. First, the while expression or condition, <expression>, is evaluated. If True, the <statement> is executed and the <expression> is evaluated again, etc. If at any time the <expression> evaluates to False, the loop is terminated and control passes to the statement after the while statement. This control flow for a while statement is shown in Figure 2.10.

To use the while statement to implement the algorithm above, there are several points to note about loops. The loop variable(s), i.e. variables used in the expression, must be initialized prior to the loop; otherwise, the loop expression is evaluated with unknown (garbage) value(s) for the variable(s). Second, if the loop expression is initially True, the loop variable(s) must be modified within the loop body so that the expression eventually becomes False. Otherwise, the loop will be an infinite loop, i.e. the loop repeats indefinitely. Therefore, a proper loop requires the following steps:

initialize loop variable(s)
     while ( <expression> ) {
          ...
          update loop variable(s)
     }

Keeping this syntax and semantics in mind, the code for the above algorithm fragment using a while loop is shown in Figure 2.11.

First, count is initialized to zero and tested for loop termination. The while statement will repeat as long as the while expression, i.e. (count < 10), is True. Since count is 0, the condition is true, so the body of the loop is executed. The loop body is a block which reads data, calculates pay, prints results, and increases the value of count by one. Except for updating count, the statements in the loop body are the same as those in the previous program in Figure 2.9. The count is updated by the assignment statement:

count = count + 1;
In this statement, the right hand side is evaluated first, i.e. one is added to the current value of count, then the new value is then stored back into count. Thus, the new value of count is one greater than its previous value. For the first iteration of the loop, count is incremented from 0 to 1 and the condition is tested again. Again (count < 10) is True, so the loop body is executed again. This process repeats until count becomes 10, is False, and the while statement is terminated. The program execution continues to the next statement, if any, after the while statement.

The above while loop is repeated ten times, once each for count = 0, 1, 2, ..., 9. We can also count the number of iterations to be performed as follows:

count = 10;
     while (count > 0) {
          ...
          count = count - 1;
     }
The initial value of count is 10 and the loop executes while (count > 0). Each time the loop is processed, the value of count is decremented by one. Eventually, count becomes 0, (count > 0) is False, and the loop terminates. Again, the loop is executed ten times for values of count = 10, 9, 8, ..., 1.

We can easily adapt the second approach to process a loop as many times as desired by the user. We merely ask the user to type in the number of people, and read into count. Here is the skeleton code.

printf("Number of people: ");
     scanf("%d", &count);
     while (count > 0) {
          ...
          count = count - 1;
     }
We use the latter approach to implement the program for our task. The entire program for pay3.c is shown in Figure 2.12

A sample session from the execution of this program is shown below.



Previous: 2.5.3 More on Expressions
Up: 2.5 More C Statements
Next: 2.5.5 Controlling Loop Termination
Previous Page: 2.5.3 More on Expressions

tep@wiliki.eng.hawaii.edu
Tue Aug 16 14:01:55 HST 1994