Loops in C++ – Complete Tutorial
Last Updated on: 6th Dec 2025 19:02:07 PM
1. Introduction to Loops in C++
In programming, many tasks require repeating the same set of instructions multiple times. Writing the same code again and again is inefficient and error-prone. To solve this problem, C++ provides loops.
Loops are used to:
-
Reduce code duplication
-
Automate repetitive tasks
-
Process collections of data
-
Build logic-based programs
2. Why Loops Are Important
Without loops:
-
Programs would be lengthy and hard to maintain
-
Repeated logic would require manual copying
-
Processing arrays or large data would be impossible
Real-Life Examples
-
Printing roll numbers of all students
-
Counting total marks
-
Displaying table of a number
-
Repeating menu options
-
Iterating over lists or arrays
What is a Loop?
A loop is a control statement that executes a block of code repeatedly while a specified condition remains true (or until a termination condition is reached). Each repetition is called an iteration. Loops have three parts: initialization, condition check, and update (progress toward termination).
3. Types of Loops in C++
C++ provides three primary types of loops:
-
for Loop
-
while Loop
-
do–while Loop
Additionally:
-
Nested Loops
-
Loop control statements: break, continue, goto
4. for Loop in C++
The for loop is used when the number of iterations is known in advance. It combines initialization, condition checking, and increment/decrement in a single line.
Syntax
for (initialization; condition; increment/decrement)
{
// code block
}
Working
-
Initialization executes once
-
Condition is checked
-
If true → execute code
-
Increment/decrement occurs
-
Steps repeat until condition becomes false
Example: Print 1 to 5
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
cout << i << " ";
}
return 0;
}
Output
1 2 3 4 5
Explanation
-
int i = 1 — initialization
-
i <= 5 — condition checked before each iteration
-
++i — update at end of each iteration
Real-World Example: Multiplication Table
int num = 5;
for (int i = 1; i <= 10; i++) {
cout << num << " x " << i << " = " << num * i << endl;
}
5. while Loop in C++
The while loop executes a block of code as long as the condition is true. The condition is checked before execution.
Syntax
while (condition)
{
// code
}
Working
-
Condition checked first
-
If true → code executes
-
Loop repeats until condition becomes false
Example: Print Numbers
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 5) {
cout << i << " ";
i++;
}
return 0;
}
Output
1 2 3 4 5
Use case
Use while when you don’t know how many times you will loop (e.g., read until EOF, wait for user input).
6. do–while Loop in C++
The do–while loop executes the code at least once, even if the condition is false.
Syntax
do
{
// code
}
while (condition);
Working
-
Code executes first
-
Condition is checked later
-
Guaranteed one execution
Example:
#include <iostream>
using namespace std;
int main() {
int i = 1;
do {
cout << i << " ";
i++;
} while (i <= 5);
return 0;
}
Use Case
-
Menus
-
Validation loops
7. Nested Loops in C++
A nested loop is a loop inside another loop. The inner loop completes fully for each iteration of the outer loop.
Syntax
for ( ; ; ) {
for ( ; ; ) {
// code
}
}
Example: Star Pattern
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
Output
*
* *
* * *
* * * *
* * * * *
8. Infinite Loops
A loop that never terminates is called an infinite loop.
Example
while (true) {
cout << "Hello";
}
9. Full Example Programs (copy-paste ready)
Example A — Sum of array using for
#include <iostream>
using namespace std;
int main() {
int a[] = {1,2,3,4,5};
int n = sizeof(a)/sizeof(a[0]);
int sum = 0;
for (int i = 0; i < n; ++i) sum += a[i];
cout << "Sum is " << sum << '\n';
return 0;
}
Example B — Menu loop ( do-while )
#include <iostream>
using namespace std;
int main() {
int choice;
do {
cout << "1. Hello\n2. Exit\nEnter: ";
cin >> choice;
if (choice == 1) cout << "Hello!\n";
} while (choice != 2);
cout << "Goodbye\n";
return 0;
}
Example C — Read ints until zero ( while )
#include <iostream>
using namespace std;
int main() {
int x;
cout << "Enter numbers, 0 to stop:\n";
while (cin >> x && x != 0) {
cout << "You entered: " << x << '\n';
}
return 0;
}
10. Practical Programs Using Loops
-
Print numbers 1 to 100 using a for loop.
-
Print multiplication table of a number.
-
Sum of numbers
-
Factorial
-
Fibonacci series
-
Prime number
-
Reverse a number
-
Palindrome check
-
Print multiplication table of a number.
11. Quick Reference Table
| Loop Type | Condition Check | Runs at least once? | Typical Use |
|---|---|---|---|
for |
before each iter | No | Known iteration count |
while |
before each iter | No | Unknown count, pre-check |
do…while |
after each iter | Yes | Menu, guarantee one run |
12. Conclusion
-
Loops are fundamental for repetition and automation.
-
Use the right loop for the scenario: for when count is known, while when waiting on condition, do…while when you need one guaranteed run.
-
Prefer range-based for for readability with containers.
-
Use break and continue judiciously.
-
Watch out for infinite loops and performance inside loops.
Keep practicing — you're doing amazing!
Happy Coding! ![]()