All the While

C provides a pre-test loop structure. The basic form of this statement is:
while( condition )
   statement

For example,
x = 5;
while(x > 0)
   x--;

will count x down from 5 to 0. (Since it doesn't do anything else, these statements could be replaced by the statement, x = 0; and the program would function the same way.) The loop:
a = 1;
while(a < b)
   a *= 2;

is a little more useful. It finds the smallest power of two that is greater than or equal to b .

Just as for the if statement, the body of the while may be a compound statement instead of just a single statement. For example, one approach to a counted loop would look like:


i = 0;
while(i < 10) {
   printf("%d\n", i);
   i++;
}

This code fragment will print out the values 0 to 9.
What will y equal after this code fragment is executed?
y = 0;
x = 11;
while(x > y)
   y += 3;

0

11

3

12