The while loop in C++ is an entry-controlled loop that executes a block of code repeatedly as long as a specified condition remains true. It is commonly used when the number of iterations is not known in advance.
- The condition is checked before each iteration.
- Used when the number of iterations is not known beforehand.
#include <iostream>
using namespace std;
int main() {
// while loop to print numbers from 1 to 5
int i = 0;
while (i < 5) {
cout << "Hi" << endl;
i++;
}
return 0;
}
Output
Hi Hi Hi Hi Hi
Explanation: The loop continues executing as long as i < 5. After each iteration, the value of i is incremented. When i becomes 5, the condition evaluates to false and the loop terminates.
Syntax
while (condition) {
// Body of the loop
update expression;
}
Components of while Loop
- Condition: Determines whether the loop should continue executing.
- Loop Body: Contains the statements that execute in each iteration.
- Update Expression: Modifies the loop variable to eventually satisfy the termination condition.
Working of while Loop
The working of the while loop can be understood using the below image:

- Control enters the while loop.
- The condition is tested.
- If true, execute the body of the loop.
- If false, exit the loop.
- After executing the body, update the loop variable.
- Repeat from step-2 until the condition is false.
Flow Diagram of while loop

Examples of while Loop in C++
The below examples demonstrate how to use while loop in our C++ programs:
Printing Numbers from 1 to 5
The following program prints numbers from 1 to 5 using a while loop.
#include <bits/stdc++.h>
using namespace std;
int main() {
// Declaration and Initialization of loop variable
int i = 1;
// while loop to print numbers from 1 to 5
while (i <= 5) {
cout << i << " ";
// Updating loop varialbe
i++;
}
return 0;
}
Output
1 2 3 4 5
Explanation: The loop starts with i = 1 and continues until i becomes greater than 5.
Calculating the Sum of Natural Numbers
The following program calculates the sum of the first 5 natural numbers.
#include <iostream>
using namespace std;
int main() {
int n = 5;
int sum = 0;
// while loop to calculate the sum
while (n > 0) {
sum += n;
n--;
}
cout << sum;
return 0;
}
Output
15
Explanation: The loop repeatedly adds the current value of n to sum and decrements n until it reaches 0.
Print a Square Pattern using Nested Loops
A while loop can be nested inside another while loop to create patterns.
#include <iostream>
using namespace std;
int main()
{
int i = 0;
// Outer loop to print each row
while (i < 4) {
int j = 0;
// Inner loop to print each character
// in each row
while (j < 4) {
cout << "* ";
j++;
}
cout << endl;
i++;
}
return 0;
}
Output
* * * * * * * * * * * * * * * *
Explanation: The outer loop controls the rows, while the inner loop prints four asterisks in each row.
Infinite while Loop
An infinite loop is created when the loop condition always evaluates to true.
#include <iostream>
using namespace std;
int main() {
// Infinite loop
while (true) {
cout << "gfg" << endl;
}
return 0;
}
Output
gfg
gfg
gfg
...
Explanation: Since the condition true never becomes false, the loop continues executing indefinitely.