while loop in C – Part 1

Till now we have learnt about decision control instructions or the programs which will execute as the way they are programmed i.e. sequentially. However its not compulsory that we want to do some task inside a program for only one time.

Suppose we want to make a program which will print your name 5 times. Then it will be very ineffective way to use the printf() function for 5 times. Instead of it we should look for something that will do that task for specific number of times automatically.

Well this is indeed possible. This is where loops comes in action. Loops are generally used to perform some task specific number of times. Similar to decision control instructions, in loops we also have to give some condition. And the program will execute task until the condition turns to be true.

There are three types of loops in C programming.

1. while
2. for
3. do while

while loop in C

This is the first loop and the second most commonly used loop in programs. It is generally used to execute a set of statements fixed number of times. Generally programmers use this loop when they already know how many times the task should be executed.

Syntax of while loop

initialize loopcounter
while (condition)
{
execute this
and this…
increment loop counter
}

What is loop counter?

As I told you earlier that compiler doesn’t know how many times you want to perform some task. So to instruct the compiler we use loop counter. As its name suggests, it counts and instruct the while loop to stop executing the task.

Flowchart of While Loop in C
Flowchart of While Loop in C – Image Source

Lets try to understand while loop with a simple program.

C program to display a name 5 times on the screen
#include<stdio.h>

void main()
{
 int x=0;
 while (x<5)
 {
  printf("TheCrazyProgrammer.com n");
  x=x+1;
 }
}

Output

Output

Explanation
  • In our program x is working as a loop counter. While declaring as a int variable I assigned a value 0 to it.
  • After that I wrote the while loop with condition x<5. It means the loop will be executed till x is less than 5. To stop the loop x should contain the value 5.
  • In the while loop I have given a printf() function to display the name. 
  • After that I have increased the loop counter by adding 1 to it.

Execution of Program
  • In the beginning compiler will check the condition to enter the while loop. But as I have assign the value 0 to it. So the condition will turn true.
  • After that the control will shift inside the loop and it will display the message.
  • Now the loop counter which is x increased by 1. Then the new value of x will be 1.
  • Now the compiler again check the condition of while loop. Now x contains the value 1 which is still true. Now it will again print the message and the value of x will again increase.
  • The process will be carried out until x becomes 5. When x will become 5 then compiler again check the condition but this time it will return false and the while loop will end.

4 thoughts on “while loop in C – Part 1”

Leave a Comment

Your email address will not be published. Required fields are marked *