There are different types of situations when loops are involved, as shown in FIGURE 5.1:
while (condition) statementwhere the (compound) statement is repeatedly executed while the condition remains true. The "condition" used here is as defined in the previous chapter.
The program fragment in FIGURE 5.2 contains a while statement, which repeatedly computes and displays the payments for seven employees. The process is also represented by the flowchart in Fig. 5.3:
To use a while loop, the control variable usually needs to be (1) initialized, (2) tested, and (3) updated. It is important to make sure that the loop will eventually terminate.
C allows "compound assignment operators", which carry out operations on variables, and keep the results in the same variables. That is,
variable op= expression;is equivalent to
variable = variable op (expression);where "op" can be any arithmetic operator.
For example, the program fragment in FIGURE 5.5 does the payment computing using a for loop.
A for statement often uses the increment and decrement operators. For an int variable i, i++ and ++i both increase the value in i by 1; i-- and --i both decrease the value in i by 1. The value of the expressions i++ and i-- is the value before the change; the value of the expressions ++i and --i is the value after the change.
For example, the program fragment
int n = 4; printf("%2d", --n); printf("%2d", n);displays " 3 3"; the program fragment
int n = 4; printf("%2d", n--); printf("%2d", n);displays " 4 3".
For example, the program in FIGURE 5.7 computes the factorial of a given integer.
The program in FIGURE 5.8 computes and displays a table of values, where the step-size of updating is not 1.
For example, if all valid observed values are non-negative, then the following loop will skip the invalid ones and warn the user:
printf("Enter number of observed values> "); scanf("%d", &num_obs); while (num_obs < 0) { printf("negative number invalid; try again> "); scanf("%d", &num_obs); }The program will repeat the request until the user provides a valid input value. This loop is conditional because if the input is valid, the loop is skipped.
The program in FIGURE 5.9 monitors the gasoline supply in a storage tank. It contains a loop that processes the "normal" removals, until a minimum amount is reached. This loop is conditional, and in advance we don't know when it will stop.
1. get a data item; 2. while the sentinel value has not been encountered 3. process the item; 4. get another data item;Please note that line 1 is necessary for the condition of line 2 to be checked for the first item.
An example of sentinel-controlled loop is in FIGURE 5.10.
It is possible to replace the while loop (with the two statements before it) in the previous program by the following for loop (with the statement before it):
printf("Enter the first score (%d to quit)> ", SENTINEL); for (scanf("%d", &score); score != SENTINEL; scanf("%d", &score)) { sum += score; printf("Enter next score (%d to quit)> ", SENTINEL); }When the input data is read from a file, a sentinel-controlled loop can be used if the number of lines to be read is unknown in advance. When scanf reaches the end of the file, it returns a standard constant EOF, which is a negative integer. For example, see FIGURE 5.11.
If the loop condition of the previous program is changed into "input_status == 1", then the loop also stops at invalid data (which will cause scanf to return 0).
FIGURE 5.13 is a program containing a double for loops, where the inner loop is controlled by the outer loop, by using the current control-variable value of the outer loop as the bound of the control-variable of the inner loop.
do statement while (condition)For a complicated loop condition, it is often convenient to keep the loop condition in an int variable, with 1 for true (continue) and 0 for false (stop). Such a variable is called a "flag".
For example, the function in FIGURE 5.14 contains a double do-while loops, and the outer loop is controlled by a flag.
Such a program needs to pass a function as a parameter, as in FIGURE 5.16. The complete program is in FIGURE 5.19.
To debug without a debugger, it is often helpful to insert some diagnostic calls, that is, printf statements displaying key variable values. To turn all of the diagnostic calls on or off at once, they can be put in if statements with a debug switch variable as condition.
Bugs in loops often appear at the boundary, that is, the first or last time of repetition.
Do not forget the two ";" in the header of a for statement.
Do not forget to put "{ }" around a loop body.
Make sure every loop will eventually terminate.
Don't mistype the "==" in a loop condition as "=".
Do not use increment, decrement, or compound assignment operators in complex expressions.