Iteration statements are used to create loops in the program. In other words, it repeats the set of statements until the condition for termination is satisfied.
There are mainly two types of loops(Iteration statements):
Entry Controlled Loops
The test situation is tested before entering the loop frame. For loop and at the same time as loop are entry-managed loops.
Exit Controlled Loops
The check condition is tested or evaluated at the end of the loop body on this kind of loop. Therefore, the loop frame will execute at least once, whether the condition is authentic or false. The do-while loop is an exit-controlled loop.
Types Of Entry Controlled Loop
The for Loop
A for loop is a repetition manipulate structure for loop is a repetition control structure that lets us write a circle that is performed a particular variety of instances. The loop enables us to perform n number of steps together in one line.
Syntax:
for(initialize; condition; update)
{
//body of the for loop
}
You can also try this code with Online C++ Compiler
Note that initializes, situations, and replaces are optional expressions and are always specified in parentheses. All of the three words are separated using semicolons. The semicolons are mandatory and, for this reason, can't be excluded even if all of the three expressions are unnoticed.
The following example:
#include <iostream>
using namespace std;
int main()
{
int i, j;
for ( i = 5, j = 10 ; i + j < 20; i++, j++ ) {
cout << "i + j = " << (i + j) << '\n';
}
}
You can also try this code with Online C++ Compiler
The loop is used to perform looping operations in situations where the number of iterations is not recognized in advance. Unlike the for-loop, the while loop is non-deterministic.
Syntax
while(condition)
{
// body of while loop
}
You can also try this code with Online C++ Compiler
Unlike loops in which initialize and replace expressions are specific, loops no longer specify any express initialize and replace phrases. This means that the control variable should be declared and initialized in advance of the while loop and needs to be up to date inside the frame of the same time as the loop.
The while loop executes so long as the condition evaluates to be authentic. If the condition evaluates to false inside the first new release, the frame of the while loop never executes.
While a loop can have more than one expression in its condition, such multiple terms must be separated using commas and are done within the order in their look.
The following example
#include <stdio.h>
Using namespace std;
int main()
{
int i = 1;
while (i < 7)
{
printf( "Coding Ninjas\n");
i++;
}
return 0;
}
You can also try this code with Online C++ Compiler
In the do-while loops, the loop execution is terminated on the idea of the test condition. The principal difference between the do-while loop and the while loop is that in the do-while loop the situation is tested on the quit of loop frame, i.e., do at the same time as the loop goes out managed whereas the other loops are entry controlled loops. In the do-while loop, the loop body will execute at least as soon as the test condition.
Syntax:
do
{
statement ;
}
while(test-expression);
You can also try this code with Online C++ Compiler
Let's watch a quick video on Loops in C++ to fully comprehend whatever we have read so far, and don't forget to follow Data Structures and Algorithms in C++, a free and comprehensive video series by Coding Ninjas, that covers each topic thoroughly.
Frequently Asked Questions
In programming, what is iteration? Iteration is the repetition of steps within a program.
Why is iteration necessary? Iteration is essential because it simplifies code by removing duplicated steps.
Which statements are used to implement entry-controlled iteration? FOR and WHILE statements are used to implement iteration.
What are the two types of iteration? The two types of iteration are Entry-controlled loops and Exit-controlled loops.
Key Takeaways
We learned about loops in C++ and their types. We saw in detail how to use for, while & do-while Loops and their syntax.