Home > C++ > Loops in C++

Loops are provided by c++ to repeat a particular block of code or set of statements any number of times. i.e. if you want a particular set of statements to be repeated again and again then you can use loops for it. Here this thing is to be noted that loop(s) are different from functions as the loop(s) will repeat same code (once loop starts its execution) unless given condition is false. On the other hand, functions only repeat the block of code whenever we call a function.

Loop(s) work on the basis of some conditions. Which somehow resemble conditional statements. Below examples will clarify their concept.

There are 3 different types of loops in c++:

While: –

This loop will run while the given condition is true. It checks the condition of the loop before executing the body of a loop.
This loop runs 0 or more times.
The basic syntax is:

initialize;
while(condition)
{
     //loop body
     Increment/decrement;
}

While Loop Example


#include "stdafx.h"
#include "iostream"

using namespace std;

void main()
{
     int x =1;
     while (x < 10)
     {
          cout << "This is a while loop \n";
          x++;
     }
     getchar();
}

Output:
while loops

Do-while: –

It is almost same as the while loop. The main difference between the two is :

  • While loop runs for 0 or more times and do while loop runs for 1 or more times
  • In do_while  loop checks condition after executing the loop body.

The basic syntax is:

initialize;
do
{
     //loop body
     Increment/decrement;
}while(condition);

Do While Loop Example

#include "stdafx.h"
#include "iostream"

using namespace std;

void main()
{
     int x =1;
     do
     {
          cout << "This is a do-while loop \n";
          x++;
     }while (x < 10);
     getchar();
}

Output:
do while loops

For: –

This is a loop used when the number of iterations for a block of code is already known.In this loop, loop variable is initialized then the condition is checked. If condition is true the body of loop is executed and at the end, variable is incremented or decremented
Basic Syntax is:

for(initialize; condition; Increment/decrement)
{
     //loop body
}

For Loop Example

#include "stdafx.h"
#include "iostream"

using namespace std;

void main()
{
     for(int x=1;x<10;x++)
     {
          cout << "This is a for loop \n";
     }
     getchar();
}
 

Output:
for loop c++
There is another variation in its syntax like:

for(initialize; condition;)
{

     //loop body
Increment/decrement;
}

For Loop C++ Example

#include "stdafx.h"
#include "iostream"

using namespace std;

void main()
{
     for(int x=1;x<10;)
     {
          x++;
          cout << "This is a for loop \n";
     }
     getchar();
}
 

Output:
for loop example
The difference between two is that in the first syntax increment/ decrement statement is always executed at the end of loop body while in second syntax it will be executed wherever it is written inside the loop body.

Nested Loops:-

Nested loops mean loop(s) inside another loop(s).
Their syntax is:
While:

initialize;
while(condition)
{
     //Outer loop body
     initialize;
     while (condition)
     {
          //Inner loop body
          Increment / decrement;
     }
     Increment / decrement;
}

Do-While:-

initialize;
do
{
     //Outer loop body
     initialize;
     do
     {
          // Inner loop body
          Increment / decrement;
     }while (condition);
          Increment / decrement;
}while (condition);

For:-

for (initialize; condition; Increment / decrement)
{
     //Outer loop body
     for (initialize; condition; Increment / decrement)
     {
          //Inner loop body
          // Increment/decrement can be done inside loop body as well
     }
     // Increment/decrement can be done inside loop body as well
}

Note: -Different no. of loops can also be used inside another loop (s) as nested loops.

Infinite Loops:-

Loops can be infinite. i.e. loop condition is never false. C++ provides various ways to define an infinite loop some are given below:
E.g.
While: –

int x = 1;
     while (x>1)
     {
          //loop body
          x++;
     }

do-while: –

     int x = 1;
     do
     {
          //loop body
          x++;
     }while (x>1);

Mostly used is:
for: –

     for ( ; ; )
     {
          //loop body
     }

Some other variations in loop usage are:

E.g.

#include "stdafx.h"
#include "iostream"

using namespace std;

void main()
{
     int x = 1;
     cout << "Outside Loop\n";
     while (x<10); // this will consider that loop has no body
     {
     cout << "inside Loop\n";
     x++;
     }
     getchar();
}

Output:
variation in loops

#include "stdafx.h"
#include "iostream"

using namespace std;

void main()
{
	
     cout << "Outside Loop\n";
     for (int x = 1;x < 10;x++); /* this will execute loop body once as after increment it will 
                                   Find ; which indicates end of loop body*/ 
     {
	     cout << "inside Loop\n";
     }
     getchar();
}

Output:
variation in loops

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.