Dremendo Tag Line

Loops in C++ Programming

Loops in C++

In this lesson, we will learn about the loops in C++ programming and name of the 3 different types of loops.

What is Loop

Loop in C++ programming come into use when we need to repeatedly execute a block of statements. For example: Suppose we want to print Hello World 10 times on the screen. This can be done in two ways as shown below:

Iterative Method

In Iterative method we have to write the cout statement 10 times in our program as shown below.

Example

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    cout<<"Hello World\n";
    cout<<"Hello World\n";
    cout<<"Hello World\n";
    cout<<"Hello World\n";
    cout<<"Hello World\n";
    cout<<"Hello World\n";
    cout<<"Hello World\n";
    cout<<"Hello World\n";
    cout<<"Hello World\n";
    cout<<"Hello World";
    return 0;
}

Output

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

Here you can see that we have written Hello World 10 times using cout() statement. So, this will print Hello World 10 times on the screen. But if want to print Hello World 100 times on the screen then the iterative method is not suitable for this. To do this we have to use loop in our program.

Loop Method

In the Loop method, we do not have to write the Hello World 10 times in our program rather we have to write Hello World only once and the loop will execute the statement 10 times.

Types of Loop in C++

There are 3 types of loop in C++ and they are:

  • for Loop
  • while Loop
  • do while Loop

In our next lessons we will learn more about these loops in great detail with examples.

video-poster