-
Hajipur, Bihar, 844101
In C++, loops are used to execute a block of code repeatedly until a specific condition becomes false. The while loop is one of the simplest and most commonly used looping structures.
It allows a program to repeat a task as long as a given condition remains true. This makes it ideal for situations where you don’t know in advance how many times you’ll need to repeat an operation.
The general syntax of a while loop is:
while (condition) {
// code to be executed repeatedly
}
The condition is evaluated before each iteration.
If the condition is true, the loop executes the code inside the braces {}.
If the condition becomes false, the loop stops.
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 5) {
cout << "Number: " << i << endl;
i++;
}
return 0;
}
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Here, the loop runs five times because i starts at 1 and stops when i exceeds 5.
Initialization: The loop variable is initialized before the loop begins.
Condition Check: Before each iteration, the condition is checked.
Execution: If the condition is true, the code inside the loop runs.
Update: The loop variable must be updated to avoid infinite loops.
Termination: When the condition becomes false, the loop stops.
Start → Condition → True → Execute Loop → Update → Condition
↓
False
↓
End
The condition in a while loop must eventually become false, or else the program will run forever.
This is called an infinite loop, and it often happens when the loop variable isn’t updated correctly.
Example of an Infinite Loop:
int x = 1;
while (x <= 5) {
cout << x << endl;
// missing x++
}
This loop never ends because x never changes.
#include <iostream>
using namespace std;
int main() {
int num = 2;
while (num <= 10) {
cout << num << " ";
num += 2;
}
return 0;
}
Output:
2 4 6 8 10
The program prints all even numbers from 2 to 10.
#include <iostream>
using namespace std;
int main() {
int i = 1, sum = 0;
while (i <= 10) {
sum += i;
i++;
}
cout << "Sum of first 10 natural numbers: " << sum;
return 0;
}
Output:
Sum of first 10 natural numbers: 55
This example shows how while loops are useful for cumulative calculations.
#include <iostream>
using namespace std;
int main() {
int num, reversed = 0;
cout << "Enter a number: ";
cin >> num;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
cout << "Reversed number: " << reversed;
return 0;
}
Output Example:
Enter a number: 1234
Reversed number: 4321
This is a practical use case of while loops — processing digits one by one.
A while loop can also control repeated user input, such as validating data.
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age (0 to exit): ";
cin >> age;
while (age != 0) {
if (age < 18)
cout << "You are a minor.\n";
else
cout << "You are an adult.\n";
cout << "Enter your age (0 to exit): ";
cin >> age;
}
cout << "Program ended.";
return 0;
}
Output Example:
Enter your age (0 to exit): 17
You are a minor.
Enter your age (0 to exit): 25
You are an adult.
Enter your age (0 to exit): 0
Program ended.
This program continues running until the user enters 0.
The condition can also be a Boolean expression that determines whether the loop should continue.
Example:
bool keepRunning = true;
int count = 1;
while (keepRunning) {
cout << "Loop " << count << endl;
count++;
if (count > 3)
keepRunning = false;
}
Output:
Loop 1
Loop 2
Loop 3
Here, the Boolean flag keepRunning controls the loop’s execution.
A nested while loop means one while loop inside another. It’s often used for matrix operations or pattern printing.
Example:
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 3) {
int j = 1;
while (j <= 3) {
cout << "(" << i << "," << j << ") ";
j++;
}
cout << endl;
i++;
}
return 0;
}
Output:
(1,1) (1,2) (1,3)
(2,1) (2,2) (2,3)
(3,1) (3,2) (3,3)
Nested loops are powerful but should be used carefully to avoid unnecessary complexity.
Both loops repeat a block of code, but there’s a key difference:
while loop checks the condition before running the block.
do...while loop checks the condition after running the block once.
Example Comparison:
int i = 5;
while (i < 5) {
cout << "Inside while loop\n";
}
do {
cout << "Inside do-while loop\n";
} while (i < 5);
Output:
Inside do-while loop
The do-while loop runs at least once even if the condition is false.
Forgetting to update the loop variable – causes an infinite loop.
Incorrect condition – may stop the loop too early or never stop.
Using semicolon after while condition – creates an empty loop accidentally.
while (i < 5); // Wrong
Not initializing the loop variable properly before entering the loop.
The while loop in C++ is a fundamental control structure used for repeated execution based on a condition.
It’s ideal when the number of iterations isn’t known in advance.
By carefully managing the condition, initialization, and update, you can perform powerful repetitive tasks efficiently.
While loops are especially useful in situations like user input validation, counting, data processing, and algorithm design.
Write a C++ program to print numbers from 1 to 100 using a while loop.
Create a program to display all even numbers between 1 and 50 using a while loop.
Write a program that calculates the sum of digits of a given number using a while loop.
Write a C++ program to find the factorial of a number using a while loop.
Create a program that counts the number of digits in an integer using a while loop.
Write a program to reverse a given integer using a while loop.
Develop a C++ program that checks whether a number is a palindrome using a while loop.
Write a program that displays the multiplication table of a given number using a while loop.
Write a program to find the largest digit in a given number using a while loop.
Create a program that keeps taking positive numbers from the user and stops when the user enters zero, then displays their sum.