C++ Loop Types

By | March 28, 2020

In this C++ tutorial, we will discuss what are loops and what are the various looping methods we use in C++

What is Loop in C++?

While programming there could be some situation when we want a block of code to execute multiple times. In every high-level programming language, we have the concept of loops.

Vamware

A loop is a statement that is capable of executing a block of code again and again until the condition of the loop becomes false or there is a break statement.

Types of Loops in C++

In C++ we can form a loop structure using three keywords

  • for
  • while
  • do…while

1. for loop

We use a for loop when we are sure that how many times, we want to execute the block of code. While defining a for loop we should take care of three thing initialization, condition and increment or decrement.

In for loop, for some situations, we can skip the initialization part but the each for loop must have the condition and increment or decrement statements. The for loop keep the block of the code executing until the condition become false or it counters a break statement.

for loop syntax:

for ( initialization ; condition ; increment or decrement )
    {
      //For loop block of code
    }

for loop example

#include <iostream>
using namespace std;

int main()
    {
       for(int i=0 ; i <5 ; i++)
          { //block of for loop starts here
            
             cout<<"This statement will print 5 times";                             //it will print 5 times
             cout<<endl;                      // to enter a new line
          } //for loop block ends here
    }

Output

This statement will print 5 times
This statement will print 5 times
This statement will print 5 times
This statement will print 5 times
This statement will print 5 times

Behind the code

In the above code we used the for loop and execute the cout<<“This statement will print 5 times”; statement 5 times.

In the above for loop example:

Initialization was int i = 0;

Condition was “execute the for loop block till i<5;”

Increment or decrement was i++

2. while loop

A while loop is similar to for loop, the while loop comprises of a condition, and the block of while loop code keeps on executing till the while loop condition is true if the loop condition becomes false or it was always false since the beginning the code of while loop block would not execute.

while loop syntax

while (condition)
       {
         //while loop block
      }

while loop Example

#include <iostream>
using namespace std;

int main()
    {
         int i=0;
         while(i<5) //while loop keep on executing till the value of i less than 5
             { //block of while loop starts here
                
                cout<<"This statement will print 5 times";                             //it will print the value of i
                cout<<endl;                      // to enter a new line
                i+=1;                      // this will increase the value of i by one for each iteration.
              } //while block ends here
   }

Output

This statement will print 5 times
This statement will print 5 times
This statement will print 5 times
This statement will print 5 times
This statement will print 5 times

Behind the code

This while loop example works the same as the above for loop was doing. Here we also have a condition (i<5) and an increment i+=1; statement. The while loop block keep executing till the value of i was less than 5 and for each iteration of while loop block we have  i+=1; statement which keep increasing the value of I by 1 so after the 5th iteration the value of I become 5 and the condition (i<5) became false and the while loop terminate.

3. do…while loop

In the do-while loop, the condition of the loop resides at the end block of the loop structure so even the condition is false the loop will execute at least one time.

do….while syntax

do
    {
         // do while block code
    } while (condition);

do…while example

#include <iostream>
using namespace std;

int main()
   {
      int i=0;
      do
            { //block of do while loop starts here
               cout<<"This statement will print 5 times";                             //it will print the value of i
               cout<<endl;                      // to enter a new line
               i+=1;                      // this will increse the value of i by one for each iteraation.
             } while(i<5);
}

Output

This statement will print 5 times
This statement will print 5 times
This statement will print 5 times
This statement will print 5 times
This statement will print 5 times

Loop Control Statements

The loop control statements are some keywords which used inside the loop block, and they are capable of altering the normal execution sequence of the loop.

In C++ we have 3 basic loop control statements or keywords:

  • break
  • continue
  • goto
1. break

The break statement terminates the loop and gets us out of the iteration sequence.

2. continue

The continue statement skip the remaining body of the loop and get back to the loop block starting point.

3. goto

the goto statement jump to the corresponding labeled statement

C++ loop Summary

  • A loop is a statement that is capable of executing the same block of code for multiple times.
  • Loop basically works on the condition, if the condition is true the loop executes if the condition is false the loop terminates.
  • In C++ we have 3 statements to create loop structure for loop, while loop, and.while loop.
  • The loop control statements used inside the loop block
  • break can terminate the loop.
  • continue skip the loop body and get back to the block starting point.
  • goto jumps to the labeled statement.

 

 

 

Leave a Reply

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