The While Loop in C is an infinite loop for the execution of a block of statements for an unknown period of time. If you are already gone through our “for loop in C” tutorial then you can start this. If not then I would like to suggest you learn first our ‘for’ loop tutorial and then come back here for better understanding. Basically, in ‘for’ loop the number of iterations is known before it enters onto the loop statement. We can say that the number of times the loop body is needed to be executed is known to us. But the while loop in C is used in a situation where we do not know the exact number of iterations of the loop before it starts the execution of code.
Like if…. Else statement, first the condition is checked then the control goes inside the body of a while loop. By the way, while loop is a very simple loop construct among the other two loop constructs and very easy to use it. It is useful when the programmer writes a simple program. Similarly, it is also used in complex programs.
The overall structure of while loop is, there must be a condition to halt the iteration of the loop. Inside the body, all statements and one or more counter variables must be there to change the value of it until the variable meets the specified condition. Basically, the counter variable is used to iterate the loop successfully. Now let’s see how the while loop works.
While Loop Working Principle in C
First, the condition tested is true then the statements in the body of the loop are executed sequentially. After one round of execution again control goes back to condition to test. If it is true, statements in the body are executed. The process goes on until the condition becomes false. When it meets the condition to be false. The control does not go inside the body of the loop. These processes are possible only due to every time change in the value of the counter variable which checks the condition until its value meets the condition to be false.
Syntax of While Loop in C programming
while(expression)
{
statement1;
Statement2;
}
Let’s have more clarity on while loop with an example.
How Does While Loop Works in C
- First, the while loop evaluates the condition of test expression inside the parenthesis () of a while loop.
- If the condition of a test expression is true, the block of statements inside the body of ‘while’ loop executed. Then the condition of test expression will be executed again.
- This process will continue until the condition of the test expression of while loop is evaluated false.
- Whenever the test expression condition will be false, then the while loop will terminate.

While Loop Flow Chart with Code
The image shown is a flow chart design with sample code at each level. Basically, the main functions and the include files will be executed first that we are not showing it here. So in ‘start’ the variable ‘i’ is getting initialized to value 1 so that we can use it in our while loop condition. In ‘Diamond’, the while loop condition will get checked. If the condition is true, then the control will enter into inside the curly brace and execute the ‘printf(“%d”,i)’. This will continue until this condition false. Once it will false, the loop will break and ends the while loop condition.
How to print the first 10 natural numbers in C
#include<stdio.h>
int main()
{
int i,k;
i=1;
while(i<=10)
{
printf(“%d”,i);
i=i+1;
}
printf(“\n”);
return 0;
}
Output:
1,2,3,4,5,6,7,8,9,10
The example tells about printing the first 10 numbers.so, instead of the same statement for 10 times, the best process is while loop implementation. That’s why while loop is implemented to understand better. Here, before entering the body of the loop, the control first checks the condition(i<=10) which is true. Then it enters the body and prints the message. Afterward, the value of I increases to iterate the execution. When the value of I becomes 11 which results false. The loop goes to the next statement i.e. printf(“\n”); in the program to execute.