While Loop in C programming language – Iteration Statements

In the previous tutorial for the For Loops we saw that apart from for loop there are two other loops that are: while loop and do-while loop. These loops basically serve the same purpose of repeating a few lines of code desired number of times. So, the question arises: Why have three different categories of loop when we are doing the same thing? Why not have just one loop?

The answer lies in the fact that all the three loops differ from each other in many aspects. One of those aspects is structure or syntax. For loop has got all the three parts of loop: initialization, test and update expression in a single line, whereas, while and do-while loops differ a lot in this aspect. These differences shall be highlighted in details in this tutorial.


While Loop

A while loop is very similar to a repeating if statement. It executes a certain block of statements based on a certain condition present at the beginning of the loop. If the condition returns boolean true the loop block is executed, otherwise not.
A while loop has its test condition at the beginning of the loop. Its initialization is done before the loop and update condition is present in the body of the loop.This flowchart explains the working of a while loop:

while-loop

Here is the syntax of a while loop:

initialization;
while(test condition)
{
  //body of the loop
  update expression;
}

Have a look at this simple program to understand how while loop works:

#include <stdio.h>
int main()
{
    int i=1;
    while(i<=5)
    {
        printf("CodinGeek\n");
        i++;
    }
    return 0;
}


Output:-
CodinGeek
CodinGeek
CodinGeek
CodinGeek
CodinGeek

This program does the same thing as the program with the for loop. But the syntax of the while loop differs a lot.


Different types of while loop

Like for loop, while loop can also be categorized into: infinite while loop, empty while loop and nested while loop. A brief discussion of all the three types of while loop is given below:

  • Infinite while loop: The while loop that keeps repeating itself an infinite number of times is known as infinite while loop. When the condition of the while loop never turns false(no termination of loop), it keeps repeating itself infinite times. unlike for loop, if we do not give any test condition to the while loop, it shows an error. Here’s an example: Here we do not give any update expression for the counter so the test condition never turns false.
int i=1;
while(i<=5)
{
  printf("CodinGeek\n");
}

The above program prints CodinGeek infinite number of times.

  • Empty while loop: An empty while loop is the one which does not have any statement or expression in its body(except the update expression). Here’s an example:
int i=1;
 while(i<=5)
 {
 i++;
 }
  • Nested While loop: A nested while loop is one which contains one or more than one loop inside its body. Here is an example of a nested while loop:
#include <stdio.h>
int main()
{
    int i=1,j;
    while(i<=3)
    {
        j=1;
        while(j<=2)
        {
            printf("CodinGeek\n");
            j++;
        }
        printf("-----\n");
        i++;
    }
    return 0;
}

Output:-
CodinGeek
CodinGeek
-----
CodinGeek
CodinGeek
-----
CodinGeek
CodinGeek
-----

In this program we see that for every time the inner loop is executed it prints CodinGeek twice (as specified in the inner loop test condition). As it exits the inner loop it goes to the outer loop and prints —– . This process is repeated thrice (specified in the outer loop test condition).

One point to be kept in consideration while writing a nested while loop is that the initialization of the inner loop should also be inside the outer loop. The common error that people make while writing nested loops is that they initialize the inner loop counter outside the outer loop body. Here’s the error example:

#include <stdio.h>
int main()
{
    int i=1,j;
    while(i<=3)
    {
        j=1;
        while(j<=2)
        {
            printf("CodinGeek\n");
            j++;
        }
        printf("-----\n");
        i++;
    }
    return 0;
}

This makes the whole inner loop run only once, i.e., The output will be displayed as:

Output:-
CodinGeek
CodinGeek
-----
-----
-----

And the outer loop works as expected. Do try not to make this mistake unless you actually want the program to give you such output. 🙂

For loop vs While loop

Despite both being entry-controlled loops,i.e., having the test condition before the body of the loop, they differ in many aspects. Here are some important differences between For loop and While loop.

  • The main point of difference between the two is their syntax which we have already seen. The position of the three parts of the loop differs for both.
  • For loops are used generally when the number of iterations is already known to us. Whereas a while loop can be used in both cases, i.e, known or the unknown number of iterations. We have a slight idea of the number of iterations in case of a while loop.
  • In case the test condition is not specified in for loop, the loop will run infinite times. Whereas, a while loop without a condition will return an error message.

An investment in knowledge always pays the best interest. Hope you like the tutorial. Do come back for more because learning paves way for a better understanding.

Do not forget to share and Subscribe.

Happy coding!! 🙂

Recommended -