The while construct

The while Statement is a general repetition statement that can be used in a programming number of environments. The general form of the while statement is

while(expression)
{
statement;
}

The statement may be a single statement or a block of statements that is to be repeated. The statement is performed while the condition is true and the program control passes to the line after the loop code, when the condition becomes false. The following program Output causes the to print the numbers from 1 to 10.

The following flowchart illustrates how the while loop works.

Example 2.6

#include <stdio.h>
int main()
{
    int number = 1; 
    while (number <= 10)
    {
        print f ("%d", number); 
        number = number + 1;
     }
}