LOOPS IN C++

In this tutorial, we will learn about different kinds of loops in C++.

Introduction to loops:

Firstly, what is a loop?

In any programming language, sometimes there would be a reason to perform a specific operation multiple times,  then instead of coding it the required times, we will use loops to iterate over the block.

For example, if you want to print any statement, you can do it in two ways, either you can write the code the number of times you required, or else you can simply put the block in any loops available in the particular programming language. This reduces the length of the code and readability, and the efficiency of the code will be increased.

So now we will look into different types of looping statements in C++

There are five kinds of loops present in C++. They are

  • For loop
  • While loop
  • Do while loop
  • Infinite loop
  • For-each loop

For loop :

 it is an iterative construct that uses the “ for “ keyword.

The general syntax of the for loop is

for( initialization; condition; increment/decrement )

{

                                     Block of code;

}

  1. First in this for loop, there are three parts. Initialization, condition, increment/decrement.
  2. First we will assign a certain value to the loop variable in the initialization process. Then the first initialized vale gets checked with the condition.
  3. If the condition satisfies, it executes the block of statements.
  4.  And after execution, it increments the value and this loop continues till the base condition satisfies

Flow chart of the for loop

Let’s see an example program implementing for loop.

#include <iostream>
using namespace std;
int main()
{
  cout<<"even numbers less than 10"<<endl;
  for(int i =2;i<=10;i +=2)
  {
cout<<i<<"\t";
  }
}

The output of the above program is:

While loop:

while loop is another looping statement that iterates over a particular block using on using base condition. ‘’ while “ keyword is used in this looping statement.

The general syntax of the while loop is

                        while( base condition )

                       {

                                 Block of statements;

}

Here in the while loop, we won’t initialize any loop variable in the looping statement. We just give a base condition to the loop and the loop will execute till the base condition satisfied

Flow chart of while loop.

Let’s see an example program on while loop.

#include <iostream>
#include <string>
using namespace std;
int main()
{
  cout<<"even numbers less than 10"<<endl;
  int i=2;
  while(i<=20)
  {
      cout<<i<<"\t";
      i += 2;
  }
}

The output of the above program.

Do while loop :

Do while loop is just a while loop with a minute modification. In do while loop, the first iteration gets executed regardless of the base condition, and after executing the first instruction, then it checks the base condition for the rest of the code.

Syntax of while loop is.

do

{

                                                   Part of do block;

} while( base condition )

The first iteration executes anyway, and then for the rest of the block, it checks the base condition of the while loop.

Flow chart of the do while loop is.

Let’s see an example program implementing the do while loop.

#include <iostream>
#include <string>
using namespace std;
int main()
{
  cout<<"even numbers less than 10"<<endl;
  int i=2;
  do
  {
      cout<<i<<"\t";
      i += 2;
  }while(i<=10);
}

The output of the above program.

Now we will understand the differences between the while loop and the do while loop.

Infinite loop :

An infinite loop, which is also known as an endless loop, occurs when any kind of loop always satisfies its base condition always. As a result, the block of statements executes indefinitely.

In C++, we will use a double semi-colon in for loop, which implies that the base condition of the for loop is always true – for(;;)

And in the case of the while loop, we use a  semi-colon or 1 or true, which implies that the while block condition is always true – while(;) or while(1) or while(true)

We should be very careful with this infinite loop implementation as it results in errors. We should have particular knowledge of executing the infinite loop.

For_each loop :

  1. Apart from the other looping statements, C++ provides another looping structure, named for_each, which allows us to use the functionality like other looping statements.
  2. This loop also takes a function as a parameter that gets executed.
  3. This looping structure is available in the header #include<algorithm>

Advantages of for_each loop:

  • Versatile. Can work with any container.
  • Readability of code increases
  • Reduced error-provoking chances

The general syntax of the for-each loop is.

for_each( beginning_position, end_position, function/object)

{

         Block of for_each loop

}

It takes three arguments.

Beginning_position: starting position from where the function starts to iterate.

end_position: ending position till where the function iterate.

Function/object: function or object which should be executed over each element.

Let’s see an example program implementing for_each loop.

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void printx2(int a)
{
    cout << a * 2 << " ";
}
int main()
{
    int arr[5] = { 1, 5, 2, 4, 3 };
    cout << "Multiple of 2 of elements are : ";
    for_each(arr, arr + 5, printx2);
    cout << endl;
   
}

The output of the above program.

Conclusion:

So that’s it from this tutorial. We have learned about different kinds of looping structures in C++. Hope you found it useful. Happy Coding!

Leave a Comment

Your email address will not be published.

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Refresh