Loops are control flow statements that execute a block of code repeatedly based on a condition. They help automate repetitive tasks without rewriting the same statements.
- Supports both condition-based and range-based loops.
- Loop execution can be modified using break and continue statements.
#include <iostream>
using namespace std;
int main(){
// Print numbers from 1 to 5
for (int i = 1; i <= 5; i++){
cout << i << " ";
}
return 0;
}
Output
1 2 3 4 5
Explanation: The for loop starts with i = 1 and prints each number until i reaches 5.
Types of Loops
C++ provides four loop forms that differ in their syntax, condition checking, and use cases.
1. for Loop
The for loop is an entry-controlled loop that checks the condition before executing its body. It is commonly used when the number of iterations is known but can also handle an unknown iteration count.
for (initialization; condition; update) {
// Loop body
}
- Initialization executes once before the loop begins.
- Condition determines whether the loop body executes.
- Update executes after each normal iteration.
Flowchart:

#include <iostream>
using namespace std;
int main() {
// For loop that starts with i = 1 and ends
// when i is greater than 5.
for (int i = 1; i <= 5; i++) {
cout << i << " ";
}
return 0;
}
Output
1 2 3 4 5
2. while Loop
The while loop is also an entry-controlled loop which is used in situations where we do not know the exact number of iterations of the loop beforehand.
Syntax:
Only the condition is part of the while loop syntax, we have to initialize and update loop variable manually.
while (condition) {
// Loop body
}
Flowchart:

#include <iostream>
using namespace std;
int main() {
int i = 1;
// while loop
while (i <= 5) {
cout << i << " ";
i++;
}
return 0;
}
Output
1 2 3 4 5
3. do while Loop
The do-while loop is an exit-controlled loop that checks the condition after executing its body. Therefore, the loop body executes at least once.
Syntax:
Like while loop, only the condition is the part of do while loop syntax, we have to do the initialization and updation of loop variablemanually.
do {
// Loop body} while (condition);
Flowchart:

#include <iostream>
using namespace std;
int main() {
int i = 1;
// do-while loop
do {
cout << i << " ";
i++;
} while (i <= 5);
return 0;
}
Output
1 2 3 4 5
4. Range-Based for Loop (For Each Loop)
Introduced in C++11, the range-based for loop iterates over elements of arrays and containers without manually managing loop counters or iterators.
Syntax:
for (data_type variable : range) {
// Loop body
}
Flowchart:

#include <iostream>
using namespace std;
int main() {
int arr[] = {1, 2, 3, 4, 5};
// Range-based for loop
for (int x : arr) {
cout << x << " ";
}
return 0;
}
Output
1 2 3 4 5
Nesting of Loops
Nesting of loops refer to placing one loop inside another. They are commonly used for pattern printing, matrix operations, and multidimensional array traversal.
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 3; i++) {
// Outer loop runs 3 times
for (int j = 0; j < 2; j++) {
// Inner loop runs 2 times for each
// outer loop iteration
cout << "i = " << i << ", j = " << j << endl;
}
}
return 0;
}
Output
i = 0, j = 0 i = 0, j = 1 i = 1, j = 0 i = 1, j = 1 i = 2, j = 0 i = 2, j = 1
Loop Control Statements
Loop control statements modify the normal execution flow of a loop.
- break Statement: Terminates the innermost enclosing loop immediately.
- continue Statement: Skips the remaining statements of the current iteration and proceeds with the next iteration, subject to the loop condition.

