Loops in C++: for, while, and do-while Explained
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.
- Art
- Causes
- Crafts
- Dance
- Drinks
- Film
- Fitness
- Food
- Games
- Gardening
- Health
- Home
- Literature
- Music
- Networking
- Other
- Party
- Religion
- Shopping
- Sports
- Theater
- Wellness