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

0
1K

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
電子煙主機SP2閃燈不停?燈號意義與對應解法!
SP2主機閃燈通常表示電量不足或功率異常。當指示燈快速閃爍時,建議立即為主機充電,並確認煙彈是否正確安裝。掌握燈號基本意義,是快速排查問題的第一步。 接觸不良導致閃燈...
By 齊 安 2025-12-12 06:47:27 0 107
Other
China Water Wastewater Treatment Equipment Market to Benefit from Rural Water Projects by 2034
China Water and Wastewater Treatment Equipment Market: Opportunities, Innovations, and Future...
By Mayuri Kathade 2025-06-09 04:20:22 0 627
Crafts
Everyday Essential Hoodie: Built for Comfort, Warmth, and Durability
When it comes to clothing that seamlessly blends comfort, warmth, and durability, few items can...
By Essential Hoodiesofficials 2025-08-28 06:18:39 0 609
Other
Ammonia Chillers
Ammonia Chillers: The Future of Sustainable Industrial Cooling Solutions Ammonia Chillers –...
By PureVibes Tech 2025-11-21 11:08:48 0 148
Other
AC Filters Cleaning: A Comprehensive Guide
Air conditioning (AC) units are essential for maintaining a comfortable indoor environment,...
By Our Services 2024-12-25 06:05:36 0 1K