Let us implement While loop in C programming and understand how a while loop works in C programming with its algorithm, flowchart, example, nested while loop and explanation.
A While loop is a control flow statement that helps to repeatedly iterate a set of code based on a given Boolean condition. The while loop can be thought of as a repeating if – else statement.
While Loop Flowchart

This looping construct follows an entry condition system wherein the condition is tested before the program control moves into the inner blocks of code within the while block.
A While loop, therefore, verifies the condition before every iteration of the loop body.
While Loop Syntax
1 2 3 4 5 | while(condition) { sequence of statements; increment / decrement; } |
The program control first validates the condition and if it results to true, then the set of instructions within the while loop is executed.
This execution of a while loop is reiterated until the condition evaluates to false. Once, the condition becomes false, the program control exits out of the While loop and executes the other statements if any.
For the while condition to change at every iteration, it is important to provide a statement within the while loop which changes the condition variables.
While Loop Algorithm
1 2 3 | WHILE test-condition Sequence END WHILE |
For this purpose, we usually get to see an increment or a decrement operator within these looping constructs. Otherwise, it becomes an infinite loop.
Irrespective of whether the do while condition is true or false, the program control executes the inner block statements at least for once and then checks the condition.
There is a difference between while and do while loop in C programming which you must know to understand the looping structures.
Note: The C program of While loop is compiled with GNU GCC compiler using CodeLite IDE on Microsoft Windows 10 operating system.
C Program To Print Numbers using While Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include<stdio.h> int main() { int count = 0, limit; printf("Enter the limit:\t"); scanf("%d", &limit); while(count < limit) { printf("%d\t", count); count = count + 1; } return 0; } |
To find more implementations of While loop in C programming, you must check these C programming examples.
Output

In order to summarize program flow of a While loop in C programming, here are the steps:
- Initialization
- Condition Validation
- Execution of block statements
- Increment/Decrement
Nested While Loop
A nested while loop is useful in many scenarios such as pattern programming, multi-dimensional arrays and much more. Let us see an example of a nested while loop in C programming.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include<stdio.h> int main() { int i = 0, j; while(i <= 5) { printf("%d\t", i); j = i + 1; while(j <= 5) { printf("%d\t", j); j = j + 1; } i = i + 1; printf("\n"); } return 0; } |
The first while loop is the outer while loop and the second while statement is known as the inner loop.
If you have any compilation errors or doubts about While loop in C programming, let us discuss in the comment section below. Find more about While loop on Wikipedia.