Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Types of Iteration Statements
2.1.
Entry Controlled Loops
2.2.
Exit Controlled Loops
3.
Types Of Entry Controlled Loop 
3.1.
The for Loop
3.2.
The while Loop
4.
Types of Controlled Exit Loop
4.1.
The do-while loop
5.
Frequently Asked Questions
6.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Iteration Statements in C++

Introduction

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.

Also, see Literals in C. Fibonacci Series in C++

Types of Iteration Statements

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
Run Code

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
Run Code

Output:

i + j = 15
i + j = 17
i + j = 19
You can also try this code with Online C++ Compiler
Run Code

 

You can try by yourself with the help of online c++ compiler.

The while Loop

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
Run Code

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
Run Code

Output:

Coding Ninjas
Coding Ninjas
Coding Ninjas
Coding Ninjas
Coding Ninjas
Coding Ninjas
You can also try this code with Online C++ Compiler
Run Code

Also Read - C++ Interview Questions

Types of Controlled Exit Loop

The do-while loop

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
Run Code

The following example:

#include <iostream>
using namespace std;
  
int main()
{
    int i = 2; 
  
    do
    {
        cout << "Coding Ninjas\n";
        i++;
   } 
   while (i < 1); 
   return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output:

Coding Ninjas
You can also try this code with Online C++ Compiler
Run Code


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

  1. In programming, what is iteration?
    Iteration is the repetition of steps within a program.
     
  2. Why is iteration necessary?
    Iteration is essential because it simplifies code by removing duplicated steps.
     
  3. Which statements are used to implement entry-controlled iteration?
    FOR and WHILE statements are used to implement iteration.
     
  4. 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. 

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problems, interview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Check out this article - Balanced Parentheses

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass