While Loop

Back - C Language Notes By Vivek Sir email: vivekdubey22@gmail.com (w) 9826424484

for loop is easy to implement if you specifically know start and end position of the loop counter. However, things in the real life are not so simple. You may come across situation where you only know when to terminate the loop. For example – reading instructions from user until terminated manually, waiting for client connection until connected or cancelled, reconnecting to the server until connected.

while loop is an entry controlled looping construct. We use while loop to repeat set of statements when number of iterations are not known prior to its execution. It provides flexibility to define loop without initialization and update parts (present in for loop).

Syntax of while loop

while(condition)

{

    // Body of while loop

}

 
Control-Flow

#include <stdio.h>

 

void main()

{

    int n = 1;

    /* Loop condition */

    while(n <= 5)

    {

        /* Body of loop */

        printf("%d ", n);

        /* Update loop counter variable */

        n = n + 1;

    }

}

 
While loop flowchart

 

Back - C Language Notes By Vivek Sir email: vivekdubey22@gmail.com (w) 9826424484