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 a 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 detail in this tutorial.


1. What is the While Loop in C?

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


2. Different types of while loops

Like for loop, while loop can also be categorized into an 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 an infinite while loop. When the condition of the while loop never turns false(no termination of the 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 an infinite number of times.

  • Empty while loop: An empty while loop is the one that 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 that 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 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 typical 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 = 1;
  while (i <= 3) {
    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. 🙂

3. For loop vs While loop in C

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

Helpful Links

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples.

All examples are hosted on Github.


Recommended Books


An investment in knowledge always pays the best interest. I 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 -

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
bahia
bahia
2 years ago

hello, the while loop example for error out of the outer loop is false , you wrote the same right example.

Hitesh Garg
Admin
2 years ago
Reply to  bahia

Thanks for pointing it out. I have updated the example.

2
0
Would love your thoughts, please comment.x
()
x
Index