The while Loop
The while statement is also used for looping. Unlike the situation with the for statement, there is only one expression field in the while statement.
The general form of the while statement is
while (expression) {
statement1;
statement2;
.
.
.
}
Here expression is the field of the expression in the while statement. The expression is evaluated first. If the expression returns a nonzero value (normally 1), the looping continues; that is, the statements inside the statement block are executed. After the execution, the expression is evaluated again. The statements are then executed one more time if the expression still returns nonzero value. The process is repeated over and over until the expression returns 0.
You see that a statement block, surround by the braces { and }, follows the while keyword and the expression field. Of course, if there is only one statement in the statement block, the braces can be discarded.
Now, let's look at an example of using the while statement. The program in Listing 7.5 is a modified version of the one in Listing 7.4, but this one uses the while statement.
TYPE
Listing 7.5. Using a while loop.
1: /* 07L05.c: Using a while loop */
2: #include <stdio.h>
3:
4: main()
5: {
6: int c;
7:
8: c = ` `;
9: printf("Enter a character:\n(enter x to exit)\n");
10: while (c != `x') {
11: c = getc(stdin);
12: putchar(c);
13: }
14: printf("\nOut of the while loop. Bye!\n");
15: return 0;
16: }
OUTPUT
The executable 07L05.exe can do a similar job as the executable 07L04.exe. The following is a copy from the screen:
C:\app> 07L05
Enter a character:
(enter x to exit)
H
H
i
i
x
x
Out of the while loop. Bye!
C:\app>
ANALYSIS
You see that the output from the execution of the program in Listing 7.5 is similar to the one from Listing 7.4, except the while statement is used in lines 10_13 of Listing 7.5.
The char variable c is initialized with a space character in line 8. Unlike the for statement in Listing 7.4, the while statement does not set c before the looping.
In line 10, the relational expression c != `x' is tested. If the expression returns 1, which means the relation still holds, the statements in lines 11 and 12 are executed. The looping continues as long as the expression returns 1. If, however, the user enters the character x, which makes the relational expression return 0, the looping stops.
The Infinite while Loop
You can also make a while loop infinite by putting 1 in the expression field, like this:
while (1) {
statement1;
statement2;
.
.
.
}
Because the expression always returns 1, the statements inside the statement block will be executed over and over—that is, the while loop will continue forever. Of course, you can set certain conditions inside the while loop to break the infinite loop as soon as the conditions are met.
The C language provides some statements, such as if and break statements, that you can use to set conditions and break the infinite while loop if needed. Details on the if and break statements are covered in Hour 10, "Getting Controls."
- 2 views