Welcome to The Coding College! In this post, we’ll explore nested loops in C++, which allow you to use loops within loops to solve more complex problems efficiently.
What are Nested Loops?
Nested loops occur when one loop is placed inside another loop. The inner loop executes completely for every iteration of the outer loop.
Syntax:
for (initialization; condition; increment/decrement) {
for (initialization; condition; increment/decrement) {
// Code to execute
}
}
How it Works:
- The outer loop controls the number of iterations.
- The inner loop completes its execution every time the outer loop runs once.
Example 1: Print a Multiplication Table
Let’s generate a multiplication table from 1
to 5
.
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 10; j++) {
cout << i << " x " << j << " = " << i * j << endl;
}
cout << endl; // Add a blank line between tables
}
return 0;
}
Output:
1 x 1 = 1
1 x 2 = 2
...
1 x 10 = 10
2 x 1 = 2
2 x 2 = 4
...
2 x 10 = 20
...
Example 2: Print a Pattern
Print the following triangle pattern using nested loops:
*
* *
* * *
* * * *
* * * * *
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter the number of rows: ";
cin >> rows;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
Output:
Enter the number of rows: 5
*
* *
* * *
* * * *
* * * * *
Example 3: Print a Rectangle of Numbers
Print a rectangle of numbers with m
rows and n
columns.
#include <iostream>
using namespace std;
int main() {
int rows, columns;
cout << "Enter the number of rows: ";
cin >> rows;
cout << "Enter the number of columns: ";
cin >> columns;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= columns; j++) {
cout << j << " ";
}
cout << endl;
}
return 0;
}
Output:
Enter the number of rows: 3
Enter the number of columns: 4
1 2 3 4
1 2 3 4
1 2 3 4
Example 4: Multiplying Two Matrices
Let’s multiply two matrices using nested loops.
#include <iostream>
using namespace std;
int main() {
const int rows = 2, cols = 2;
int mat1[rows][cols] = {{1, 2}, {3, 4}};
int mat2[rows][cols] = {{5, 6}, {7, 8}};
int result[rows][cols] = {0};
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
for (int k = 0; k < cols; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
cout << "Resultant Matrix: " << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << result[i][j] << " ";
}
cout << endl;
}
return 0;
}
Output:
Resultant Matrix:
19 22
43 50
Example 5: Prime Numbers in a Range
Find and print all prime numbers in a range.
#include <iostream>
using namespace std;
int main() {
int start, end;
cout << "Enter the start of the range: ";
cin >> start;
cout << "Enter the end of the range: ";
cin >> end;
for (int i = start; i <= end; i++) {
bool isPrime = true;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime && i > 1) {
cout << i << " ";
}
}
return 0;
}
Output:
Enter the start of the range: 10
Enter the end of the range: 20
11 13 17 19
Example 6: Inverted Triangle Pattern
Print the following pattern using nested loops:
* * * * *
* * * *
* * *
* *
*
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter the number of rows: ";
cin >> rows;
for (int i = rows; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
Output:
Enter the number of rows: 5
* * * * *
* * * *
* * *
* *
*
Best Practices for Nested Loops
- Keep it Simple: Avoid excessive nesting to maintain readability.
- Optimize When Possible: Minimize the number of iterations to improve performance.
- Indentation: Use proper indentation to make the code more readable.
- Break and Continue: Use
break
orcontinue
to exit loops early if a condition is met.
When to Use Nested Loops
- Matrix Operations: Multiplication, addition, or traversing multi-dimensional arrays.
- Patterns: Printing shapes like triangles, diamonds, or grids.
- Complex Iterations: When multiple dimensions or layered iterations are required.
Explore More at The Coding College
Visit The Coding College for more coding tutorials, patterns, and advanced concepts.
What’s Next?
- Learn about recursion for solving problems without loops.
- Explore break and continue statements to control loop behavior.
- Dive into arrays and combine them with nested loops for practical applications.