While loop in C programming

In previous post, we began our discussion on looping statements and learned for loop. In this post we will continue our discussion on while loop.

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).

Looping statements whose condition is checked prior to the execution of its body is called as Entry controlled loop.

Syntax of while loop

while(condition) 
{
    // Body of while loop
}

Parts of while loop

Unlike for loop, while does not contain initialization and update part. It contains only two parts – condition and body of loop.

  • condition is a boolean expression evaluating an integer value. It is similar to if…else condition and define loop termination condition.
  • Body of loop contains single or set of statements. It define statements to repeat.

At this point, you might be thinking about loop counter variable-initialization and variable-update part. Where to put these? You are free to initialize loop counter variables anywhere in the program before its use. However, best practice is to initialize all important loop variable just before the loop. Likewise, you can keep your loop update part just before the end of loop.

How while loop works?

Simplicity of while loop exists in its working mechanism. while loop works in two steps.

  1. Initially program control is received by condition block. It contains set of relational and logical expressions. If result of the conditional expression is 1 (true) then while transfers program control to body of loop. Else if result of conditional expression is 0 (false) then it exits from loop.
  2. Body of loop contain single or set of statements to repeat. It execute all statements inside its body and transfer the program control to loop condition block.

Step 1 and 2 are repeated until the loop condition is met.

The above two steps are repeated, until loop condition is true.

Flowchart of while loop

While loop flowchart

Example program to demonstrate while loop

Let us write a C program to print natural numbers from 1 to 10 using while loop.

/** 
* C program to print natural numbers using while loop 
*/ 
#include <stdio.h>

int main() 
{ 
    /* Loop counter variable declaration and initialization*/ 
    int n = 1; 

    /* Loop condition */ 
    while(n <= 10) 
    { 
        /* Body of loop */ 
        printf("%d ", n); 

        /* Update loop counter variable */ 
        n++; 
    }

    return 0;
}

Output –

1 2 3 4 5 6 7 8 9 10

Explanation of above program

  • The statement int n = 1; declares a loop counter variable initialized with 1.
  • Next loop condition receives program control and check condition n <= 10, which is true, hence while transfers program control to loop body.
  • Inside loop body statement printf("%d ", n); displays the current value of n i.e. 1 on console.
  • The statement n++; increment the value of n by 1. Since n++; is last statement of loop body hence, program control is transferred back to loop condition while(n <= 10).
  • Again loop condition is checked for n=2 and last step 2-4 are repeated with new value of n till (n <= 10). Once n = 11, the loop condition while(n <= 10) gets false and loop terminate.