Syntax of while loop

initialization;
while(condition)
{
    increment or decrement;
    // statements
}

initialization: An initial value is assigned to a variable in this section.
condition: Condition is checked and statements are executed inside while loop if condition is true.
increment or decrements:  Update initial value to count number of iteration and based on the update value condition will be false and loop will stop.

Example:

#include<stdio.h>
int main()
{
    int i;
    i=1;
    while(i&lt;5)
    {
        printf("Iteration %d\n",i);
        i++;
    }
    return 0;
}

OUTPUT

Iteration 1
Iteration 2
Iteration 3
Iteration 4

If you don’t understand how this while loop works then read for loop first.


Next Previous