Welcome to The Coding College! In this tutorial, we’ll dive into the while loop in C++. Loops are essential in programming for handling repetitive tasks, and the while
loop is one of the simplest yet powerful looping constructs.
What is a While Loop?
A while loop is used to repeatedly execute a block of code as long as a specified condition is true
. It’s useful when you don’t know the number of iterations in advance and need to rely on a condition to control the loop.
Syntax
while (condition) {
// Code to execute while the condition is true
}
condition
: A Boolean expression that determines whether the loop continues. If the condition isfalse
, the loop exits.- The code inside the loop is called the loop body.
Example: Simple While Loop
Let’s print numbers from 1 to 5 using a while loop:
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 5) {
cout << i << endl;
i++;
}
return 0;
}
Output:
1
2
3
4
5
Example: Sum of First N Natural Numbers
#include <iostream>
using namespace std;
int main() {
int n, sum = 0, i = 1;
cout << "Enter a positive integer: ";
cin >> n;
while (i <= n) {
sum += i;
i++;
}
cout << "The sum of the first " << n << " numbers is " << sum << "." << endl;
return 0;
}
Output Example:
Enter a positive integer: 5
The sum of the first 5 numbers is 15.
Example: Input Validation
A while loop is often used to validate user input.
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number greater than 0: ";
cin >> number;
while (number <= 0) {
cout << "Invalid input. Try again: ";
cin >> number;
}
cout << "You entered: " << number << endl;
return 0;
}
Output Example:
Enter a number greater than 0: -3
Invalid input. Try again: 5
You entered: 5
Infinite Loop
A while loop can run infinitely if the condition never becomes false
.
#include <iostream>
using namespace std;
int main() {
while (true) {
cout << "This is an infinite loop!" << endl;
}
return 0;
}
Note: Infinite loops should generally be avoided unless they are intentionally used (e.g., for servers or event listeners).
Breaking Out of a While Loop
You can use the break
statement to exit a while
loop when needed.
Example: Break Condition
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (true) {
if (i > 5) {
break;
}
cout << i << endl;
i++;
}
return 0;
}
Output:
1
2
3
4
5
Example: Find the Smallest Divisor
#include <iostream>
using namespace std;
int main() {
int n, divisor = 2;
cout << "Enter a number greater than 1: ";
cin >> n;
while (divisor <= n) {
if (n % divisor == 0) {
cout << "The smallest divisor of " << n << " is " << divisor << "." << endl;
break;
}
divisor++;
}
return 0;
}
Output Example:
Enter a number greater than 1: 15
The smallest divisor of 15 is 3.
Common Mistakes with While Loops
- Infinite Loops: Forgetting to update the condition inside the loop.
int i = 1;
while (i <= 5) {
cout << i << endl; // Infinite loop: i is never incremented
}
- Off-by-One Errors: Ensure the condition correctly defines the loop bounds.
- Unreachable Code: Placing statements after an infinite loop without a
break
.
Summary
- While loops repeat a block of code as long as the condition is
true
. - Use them when you don’t know the exact number of iterations in advance.
- Combine them with conditions and the
break
statement for more control.
Explore More at The Coding College
Discover more tutorials and examples on The Coding College. Learn programming concepts step-by-step and become a coding pro!
What’s Next?
- Learn about the do…while loop, which guarantees at least one iteration.
- Explore the for loop for fixed iterations.
- Master nested loops for advanced scenarios.