Loops in C++: for, while, and do-while Explained

0
2K

Introduction

In C++, loops allow you to execute a block of code repeatedly under a certain condition. There are three main types of loops: for, while, and do-while. Loops can be helpful to print any statement multiple times. Each type of loop is useful in different situations.

Why Use Loops?

Loop is a fundamental concept in programming that is used to repeat a block of code multiple times. Without using a loop, if you want to print any statement 100 times, you need to write this 100 times in your code.

But with the help of loops, you can print any statement multiple times easily.

Example (Without use of loop):

#include <iostream>

using namespace std;

 

int main() { 

    cout << "Hello Tpoint Tech\n";

    cout << "Hello Tpoint Tech\n";

    cout << "Hello Tpoint Tech\n";

    cout << "Hello Tpoint Tech\n";

    cout << "Hello Tpoint Tech";

    return 0;

}

Output:

Hello Tpoint Tech

Hello Tpoint Tech

Hello Tpoint Tech

Hello Tpoint Tech

Hello Tpoint Tech

Example: By using a Loop

#include <iostream>

using namespace std;

 

int main() { 

          for (int j = 1; j <= 6; j++) {

          cout << "Hello Tpoint Tech\n";

    }

    return 0;

}

Output:

Hello Tpoint Tech

Hello Tpoint Tech

Hello Tpoint Tech

Hello Tpoint Tech

Hello Tpoint Tech

Hello Tpoint Tech

C++ provides three types of loops such as for loop, while loop and do-while loop as detailed below:

1) For loop

For loop is referred to as an entry-controlled loop. It is used to execute block of code a fixed number of times. This kind of loop is suitable or ideal when you know the number of iterations in advance. This loop contains three parts such as initialization, condition and updation.

Syntax:

for (initialization; condition; updation) {

     // Write your code here

}

Example:

#include <iostream>

using namespace std;

 

int main() {

    int add = 0;

    for (int n = 1; n <= 10; n++) {

        add += n;

    }

    cout << "Addition = " << add << endl;

    return 0;

}

Output:

Addition = 55

 

2) While loop

While loop is also referred to as an entry-controlled loop. In C++, the while loop is used to repeat a block of code as long as a condition is true. It is compact and ideal when the number of iterations is not known in advance.

Syntax:

while (condition) {

    // Write your code here

    // update expression

}

Example:

#include <iostream>

using namespace std;

 

int main() {

    int num = 7543; 

    int reverse = 0;

 

    while (num != 0) {

        int digit = num % 10;         

        reverse = reverse * 10 + digit; 

        num = num / 10;            

    }

 

    cout << "Reverse number = " << reverse << endl;

 

    return 0;

}

Output:

Reverse number = 3457

3) do while Loop

The do while loop is referred to as a post-test loop. It executes a block of code at least once and then checks the condition to decide whether to continue.

Syntax:

do {
  // code block to be executed
}
while (condition);

Example:

#include <iostream>

using namespace std;

 

int main() { 

          int i = 1; 

     do {

        cout << i << " ";     

          i++;

    }while (i <= 10);

 

    return 0;

}

Output:

1 2 3 4 5 6 7 8 9 10

Nested loops (Loops in Loops)

Nested loops contain one loop inside another loop such as inner loop and outer loop. In C++, all loop types (for, while, do-while) can be nested within each other. Especially, nested loops are useful for working with 2D data structures, patterns, tables, or matrix operations.

Syntax:

for (initialization; condition; updation) {
for (initialization; condition; updation) {
// statement of inside loop
}

// statement of outer loop
}

Example:

#include <iostream>

using namespace std;

 

int main() {

    int num = 5;

 

    for (int i = 1; i <= num; i++) {

        for (int j = 1; j <= i; j++) {

            cout << "* " ;

        }

        cout << endl;

    }

    return 0;

}

Output:

*

* *

* * *

* * * *

* * * * *

Control statements in Loops

Loop control statement is used to change the normal flow of the code like skip the iteration, jump to the next iteration and break the loop.

Break statement: This statement is used to exit the loop as soon as the condition is met.

Syntax:

break;

Example:

#include <iostream>

using namespace std;

int main() {

    for (int i = 1; i <= 10; i++) {

        if (i == 7)

            break;

        cout << i << " ";

    }

    return 0;

}

Output:

1 2 3 4 5 6

Continue statement: The continue statement is used to skip the iteration of the loop when the condition is met.

Syntax:

continue;

Example:

#include <iostream>

using namespace std;

 

int main() {

    int n = 0;

 

    while (n< 10) {

        n++;

        if (n % 2 == 0)

            continue;

        cout << n << " ";

    }

 

    return 0;

}

Output:

1 3 5 7 9

Conclusion

Loops are essential in C++ for executing repetitive tasks efficiently. Understanding when and how to use each loop type makes your code more efficient, readable, and flexible, especially when dealing with data processing, pattern generation, or automation. I suggest you learn the C++ Programming language from the Tpoint Tech website as it provides C++ programming tutorials, interview questions, and an Online Compiler as well.

Search
Categories
Read More
Other
Importer of Record Massachusetts for Commercial Cargo Management | One Union Solutions
Manage Massachusetts-bound commercial cargo through Importer of Record Massachusetts from One...
By One Union Solutions 2026-08-24 18:17:03 0 277
Wellness
Design Evolution: Miniaturization and Discreet Aesthetics in the Hearing Aids Market
The continuous focus on miniaturization and aesthetic design remains a critical component driving...
By Sonu Pawar 2025-12-15 09:58:56 0 720
Other
Government Funding and Regulatory Support Accelerate Development of Next-Generation Cancer Vaccines
Polaris Market Research has published a brand-new report titled Japan Cancer Vaccines Market...
By Emma Verghise 2025-11-25 11:50:56 0 605
Games
Fortnite Shiny Llamas: How to Find & Farm Rewards
While playing Fortnite you may suddenly see shiny llamas show up during a match. For newcomers...
By Xtameem Xtameem 2026-04-21 10:47:24 0 184
Other
Book Yellow Boat Ride in Dubai Best Sightseeing Cruise Experience
Planning for a getaway with your family, friends, or a partner, Dubai is the next destination in...
By Kevin Ross 2025-04-17 06:59:42 0 2K