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.

Cerca
Categorie
Leggi tutto
Altre informazioni
Wall Putty Manufacturing Plant Cost Analysis Report 2026: Industry Trends and Unit Setup
The wall putty industry has become one of the fastest-growing segments in the construction...
By Sagar Imarc 2025-12-30 11:25:26 0 949
Giochi
Valorant Maps: Why They Get Removed & Reworked
Valorant's map pool remains intentionally small, with each design carefully scrutinized for...
By Xtameem Xtameem 2026-06-13 07:21:08 0 269
Film
Online Casino: Any Online Toy store for Fashionable Video games
  On line casinos own reshaped any gaming community by just carrying anticipation, vast...
By Toyay Toyay 2026-02-16 06:00:16 0 158
Giochi
Earning coins fast in College Football 26 Ultimate Team doesn't
With College Football 26 finally arriving, the long-awaited return of college football gaming is...
By JeansKey Zhu 2025-10-31 07:07:18 0 271
Giochi
Your Guide to Bigger EV6 Casino Wins
Discover how every bet brings you closer to huge jackpots. Learn smart strategies, top game...
By Summer Down 2025-12-09 03:45:10 0 373