LOOPING STATEMENT IN C++
INTRODUCTION
There can be situations when you need to run a code block several times. The first statement in a function is often executed first, followed by the second and so on. This is the normal order in which statements are executed. Programming language control structures provide more complex execution paths. A circle explanation permits us to over and over execute a single articulation or a set of related articulations. Within the lion's share of programming dialects, a circle explanation ordinarily takes the taking after form:
To meet looping needs, the C++ programming language supports the following types of loops.
Sr.No | Loop Type & Description |
1 | while loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. |
2 | for loop Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable. |
3 | do...while loop Like a ‘while’ statement, except that it tests the condition at the end of the loop body. |
4 | nested loops You can use one or more loop inside any another ‘while’, ‘for’ or ‘do..while’ loop. |
Loops in C++ are classified into three types: for, while, and do-while.
For the Loop:
When you know how many times the loop needs to be run, you use the for loop. It is divided into three sections ,startup, condition, and iteration.
Syntax:
for (initialization; condition; iteration) {
// code to be execute
}
The While Loop:
When you wish to repeat a block of code while a condition is true, you use the while loop.
Syntax:
while (condition) {
// code to be executed
// iteration statement (to eventually make the condition false)
}
The Do-While Loop:
While the do-while loop is similar to the while loop, it ensures that the code block is performed at least once before the condition is verified.
Syntax:
do {
// code to be executed
// iteration statement (to eventually make the condition false)
} while (condition);
The following are the essential characteristics of C++ looping statements:
C has three main forms of loops: for, while and do-while, each with its own advantages and disadvantages. Thus, developers can choose the cycle that best suits the situation at the time. Initialization, Condition, and Iteration You can initialize variables, declare a condition, and set the iteration step for loops on the same line. As a result, the for loop is a good solution in many situations.
Initialization, Condition, and Emphasis You'll be able initialize factors, announce a condition, and set the cycle step for circles on the same line. As a result, the for circle could be a great arrangement in numerous situations. Initialization, Condition, and Cycle: You'll initialise factors, announce a condition, and characterize an iteration step all on the same line in for circles. As a result, the for circle could be a great solution for many circumstances. For and whereas circles are pre-test circles, which implies they confirm the condition some time recently entering the circle body. The do-while circle could be a post-test circle that guarantees that the body of the circle is executed at slightest once some time recently the condition is checked. Circle control factors can be changed inside the circle, permitting energetic changes within the behavior of the circle.
Programme
For loop:
#include <iostream>
using namespace std;
int main() {
int num, sum;
sum = 0;
cout << "Enter a positive integer: ";
cin >> num;
for (int i = 1; i <= num; ++i) {
sum += i;
}
cout << "Sum = " << sum << endl;
return 0;
}
Output:
![LOOPING STATEMENT IN C++](https://static.tutorialandexample.com/cpp/looping-statement-in-cpp1.png)
While loop:
#include <iostream>
using namespace std;
int main () {
// Local variable declaration:
int a = 10;
// while loop execution
while( a < 20 ) {
cout << "value of a: " << a << endl;
a++;
}
return 0;
}
Output:
![LOOPING STATEMENT IN C++](https://static.tutorialandexample.com/cpp/looping-statement-in-cpp2.png)
Do- while loop:
#include <iostream>
using namespace std;
int main () {
// Local variable declaration:
int a = 10;
// do loop execution
do {
cout << "value of a: " << a << endl;
a = a + 1;
} while( a < 20 );
return 0;
}
Output:
![LOOPING STATEMENT IN C++](https://static.tutorialandexample.com/cpp/looping-statement-in-cpp3.png)
When employing a for circle, you'll characterize the initialization, condition, and cycle steps on a single line. This makes it brief and viable, particularly for circles with a settled number of redundancies.
for (int i = 0; i < 5; i++) {
// Loop body
}
Clear Loop Control: A for loop's form makes it simple to determine the loop's behaviour at a look. The header of the loop contains explicit definitions of the initialization, condition, and iteration phases.
For Count-Controlled Loops Built: It works especially effectively in scenarios when you know ahead of time how many times the loop needs to run. This is typical in situations like executing a fixed amount of calculations or iterating across arrays.
Efficient for Fixed Iterations: A for loop may be more effective than a while loop when the number of iterations is fixed. This is so because the loop header contains definitions for all loop control parameters.