In this tutorial, we will learn about the applications of loops.
Introduction:
In this article, we will implement the loops concept that we have learned in the previous article.
For example, let’s say we want to show a message 100 times. Then instead of writing the print statement 100 times, we can use a loop.
That was just a simple example; we can achieve much more efficiency and sophistication in our programs by making effective use of loops.
we have already seen the syntax and example of loops.
Let’s create a loop for printing a message 100 times.
pseudocode:
for(int i=0;i<100;i++){
cout<<"Hello Message";
}
We can use loops as:
- Range Based
- Condition Based
Range Based loops:
Range-Based loops are used when the user knows the boundaries or the number of times the block of code (body of the loop) has to repeat.
Example : for-Loop
Condition Based:
Condition-Based loops are used when we don’t the exact number of times the loop has to repeat. Hence, we place a condition to break the loop when the condition is satisfied.
Example: While-Loop, do-while-loop
We need to make sure that the loops are not running an infinite number of times which causes deadlock or starvation. In simple it crashes the process and ultimately affects the system performance.
Applications of loops in programming:
We will use loops in a programming language for multiple purposes.
- Searching Operations
- Doing a Task repeatedly
- Scrubbing Algorithms
- Traversing a list of elements
- In various algorithms.
Loops are very frequently used in programming.
From this article, we will use loops in the number of examples. So you need to brush up on the syntaxes, examples, and implementation of loops.
Nested Loops
In c++, we can use nested loops. We can write a loop inside of another loop which is similar to nested-if where we write an if-statement inside of another if-statement.
Nested loops are mainly used in:
- Traversing a multi-dimensional array
- Sorting a list of elements
- In various other algorithms
Syntax of nested loops:
syntax of nested for-loop:
for(init;cond;inc/dec) {
for(init;cond;inc/dec) {
// statement of inside for-loop
}
// statement of the outer for-loop
}
syntax of nested while-loop:
while(condition) {
while(condition) {
// statement of inside loop
}
// statement of the outer loop
}
syntax of nested do-while-loop:
do{
do{
// statement of inside loop
}while(condition);
// statement of the outer loop
}while(condition);
We can also nest one kind of loop inside the other. For example, we can nest a for loop inside of a do-while loop like the following:
do{
while(condition) {
for ( initialization; condition; increment ) {
// statement of inside for loop
}
// statement of inside while loop
}
// statement of outer do-while loop
}while(condition);
Example of nested loops:
Now, let’s look at the examples of nested loops with their implementations:
Example 1: Below program uses a nested for loop to print a 2D matrix of 3×3.
// C++ program that uses nested for loop
// to print a 2D matrix
#include <bits/stdc++.h>
using namespace std;
#define ROW 3
#define COL 3
// Driver program
int main()
{
int i, j;
// Declare the matrix
int matrix[ROW][COL] = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
cout << "Given matrix is \n";
// Print the matrix using nested loops
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++)
cout << matrix[i][j];
cout << "\n";
}
return 0;
}
Output:
Given matrix is 123 456 789
Example 2: Below program uses a nested for loop to print all prime factors of a number.
// C++ Program to print all prime factors
// of a number using nested loop
#include <bits/stdc++.h>
using namespace std;
// A function to print all prime factors of a given number n
void primeFactors(int n)
{
// Print the number of 2s that divide n
while (n % 2 == 0) {
cout << 2;
n = n / 2;
}
// n must be odd at this point. So we can skip
// one element (Note i = i +2)
for (int i = 3; i <= sqrt(n); i = i + 2) {
// While i divides n, print i and divide n
while (n % i == 0) {
cout << i;
n = n / i;
}
}
// This condition is to handle the case when n
// is a prime number greater than 2
if (n > 2)
cout << n;
}
/* Driver program to test above function */
int main()
{
int n = 315;
primeFactors(n);
return 0;
}
Output:
3357
Conclusion:
Hurray! we have successfully learned the concept of loops in c++ with implementation. Now do some exercises on loops to become more confident. Happy Coding!