Loops in C++

When we need to run a set of statements repeatedly in programming, we utilize loops.

Suppose if we want to print TechVidvan 8 times, we can write cout << “TechVidvan”; 8 times iteratively. An alternate and efficient way of doing this is to use loops. We will have to write the statement (cout << “TechVidvan”;) once inside a loop and the loop will run 8 times generating the desired result.

A loop is a set of instructions that is repeated until a given condition is met. In this article, we will be covering loops in C++.

Types of loops in C++

1. Entry Controlled loops in C++

Loops in which the condition is checked at the beginning of the loop are called Entry Controlled loops. There are 2 entry controlled loops in C++.

a.  for loop in C++

We use the for loop when we know the exact number of times we want to iterate through a section of code.

Syntax:

for (initialization; condition; update)
{    
     // body of the loop
}

Here,

  • initialization expression initializes the loop variable(s) and is executed only once.
  • condition expression states the test condition. Loop body is executed if the condition is true; if it is false, the for loop is ended.
  • update expression updates the value of loop variable(s) after execution of loop body.
Flowchart of for loop

flow chart of for loop

Example of for loop in C++

//Print first n natural numbers
#include <iostream>
using namespace std;

int main() {
  int n;
  cout<<"Enter n:";
  cin>>n;
  cout<<endl<<"First "<<n<<" natural numbers are:"<<endl;
  for(int i=1; i<=n; i++) {
      cout<<i<<" ";
  }
  return 0;
}

Output

Enter n:10
First 10 natural numbers are:
1 2 3 4 5 6 7 8 9 10
b.  while loop

while loops are useful when the exact number of loop iterations is unknown ahead of time. The loop executes as long as the test condition is true.

Syntax:

while (condition)
{
  //body of the loop
}
Flowchart of while loop

Flowchart of while loop

Example of while loop in C++

//program to print TechVidvan 5 times

#include <iostream>
using namespace std;

int main() {
  int n = 1;
  while(n<=5) {
      cout<<"TechVidvan"<<endl;
      n++;
  }
  return 0;
}

Output

TechVidvan
TechVidvan
TechVidvan
TechVidvan
TechVidvan

2. Exit Controlled Loop in C++

Loops in which the condition is checked at the end of the loop are called Exit Controlled loops. Hence, regardless of whether the condition is true or false, the loop body will run at least once.

a.  do-while loop

A variant of while loop is do – while loop. But, in do – while loop, the set of statements will execute once before the condition is checked. This is ideal in situations when the loop must run at least once.

Syntax:

do {
  //body of the loop
} while (condition);
Flowchart of do-while loop

Flowchart of do-while loop

Example of the do-while loop in C++

#include <iostream>
using namespace std;

int main() {
  int i=6;
  do {
      cout<<"TechVidvan"<<endl;
      i++;
  }while (i<5);       
  
  return 0;
}

Output

TechVidvan

3. Infinite Loop in C++

An infinite loop or an endless loop is a block of code that iterates indefinitely. This happens when the test condition evaluates to true every time. It is mostly an error.

Examples of situations of infinite loop in C++

for (int i=1; i>0; i++) {
  cout<<"Infinite loop"<<endl;
}

for ( ; ; ) {
cout<<"Infinite loop"<<endl;
}

while(true) {
  //body of the loop
}

do {
  int i = 1;
  //statements
} while(i>0);

We can terminate an infinite loop by pressing Ctrl+c.

4. Nested Loop in C++

Nested loop, also called loop inside loop, is a loop contained within another loop.

Example of Nested loop in C++

//program to print a pattern
#include <iostream>
using namespace std;

int main() {
  for(int i=0; i<2; i++) {
      for(int j=0; j<3; j++) {
          cout<<"*";
      }
      cout<<endl;
  }
  return 0;
}

Output

***
***

In this example, a for loop is nested inside another. However, different types of loops can be nested as well.

Loop Control Statements in C++

C++ offers some statements that we can use when we want to change the execution sequence of our program. All automatic objects generated in a scope are deleted when execution exits that scope.

There are three loop control statements in C++:
1. break: We use the break to terminate a loop or a switch statement. Execution shifts to the next statement after the loop.

2. continue: Continue statement skips the rest of the loop body and immediately checks the test condition before reiterating.

3. goto: We use the goto statement to pass control to the labeled statement. Although using the goto statement is not recommended.

Summary

In this article, we learnt various types of loops in C++. Loops in C++ are broadly classified into entry controlled and exit controlled. There are two entry controlled loops namely, for loop and while loop. do-while loop is an exit controlled loop. We understood the use of these loops with proper examples.

Then, we saw what infinite loops are in C++. We also covered nested loops with an example. Lastly, we learned about various loop control statements that C++ offers.