Related Articles

C++ Loops

In Programming, sometimes there is a need to perform some operation more than once or (say) n number of times. Loops come into use when we need to repeatedly execute a block of statements. 

For example: Suppose we want to print “Hello World” 10 times. This can be done in two ways as shown below: 

Manual Method (Iterative Method)

Manually we have to write cout for the C++ statement 10 times. Let’s say you have to write it 20 times (it would surely take more time to write 20 statements) now imagine you have to write it 100 times, it would be really hectic to re-write the same statement again and again. So, here loops have their role.

C++




// C++ program to Demonstrate the need of loops
#include <iostream>
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";
    return 0;
}


Output

Hello World
Hello World
Hello World
Hello World
Hello World


Time complexity: O(1)

Space complexity: O(1)

Using Loops

In Loop, the statement needs to be written only once and the loop will be executed 10 times as shown below.  In computer programming, a loop is a sequence of instructions that is repeated until a certain condition is reached. 

Similar Reads

There are mainly two types of loops:

...

For Loop-

Entry Controlled loops: In this type of loop, the test condition is tested before entering the loop body. For Loop and While Loop is entry-controlled loops. Exit Controlled Loops: In this type of loop the test condition is tested or evaluated at the end of the loop body. Therefore, the loop body will execute at least once, irrespective of whether the test condition is true or false. the do-while loop is exit controlled loop....

While Loop-

A For loop is a repetition control structure that allows us to write a loop that is executed a specific number of times. The loop enables us to perform n number of steps together in one line....

Do-while loop

...

What about an Infinite Loop?

...

Important Points

...

Related Articles:

...

Contact Us