Loop / Iteration Control Structure

  • Home
  • Loop / Iteration Control Structure
Shape Image One

Loop / Iteration Control Structure:

advanced divider

Loop / iteration control structure:

Iteration or loop in computer programming is a process wherein a set of instructions or structures are repeated in a sequence a specified number of times until a condition is true. When the set of instructions is executed again, it is called an iteration. A loop statement allows us to execute a statement or a group of statements multiple times.

C++ provides the following types of loops to handle looping requirements:

 

for loop

while loop

do-while loop

For loop:

A for loop is a repetition or iteration control structure that repeats a statement or block of statements for a specified number of times. The for-loop statement includes the initialization of the counter, the condition, and the increment. The for loop is commonly used when the number of iterations is known.

Syntax:

The general syntax of for loop is:

{

    statement(s);

}

 

For Loop Example:

#include<iostream>

using namespace std;

int main ()

{

    int i;

    for (i = 1; i <= 10; i++)

    {

        cout << i << ” “;

    }

    return 0;

}

Output:

1 2 3 4 5 6 7 8 9 10

While Loop:

The while loop allows the programmer to specify that an action is to be repeated while some conditions remain true. It is used when the exact number of loop repetitions before the loop execution begins are not known.

 

Syntax:

The general syntax of a while loop is:

 

while(condition)

{

    statement(s);

}

while Loop Example

 

#include<iostream>

using namespace std;

int main()

{

    int i = 1;

    while (i <= 10)

    {

        // The content inside the loop is missing from the extraction

    }

}

cout << i << ” “;

i++;

}

return 0;

}

 

Do while Loop:

A do-while loop is similar to a while loop, except that a do-while loop is guaranteed to execute at least one time, even if the condition fails for the first time. Unlike for and while loops, which test the loop condition at the start of the loop, the do while loop checks its condition at the end of the loop.

 

Syntax

The general syntax of a do-while loop is:

do

{

    statement(s);

}

while(condition);

 

do while Loop 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:

When one for, while or do while loop is nested within another loop, they are said to be nested. The inside loop is completely repeated for each repetition of the outside loop.

 

Nested Loop Example

 

#include<iostream>

using namespace std;

int main()

{

    int row, column;

    for (row = 1; row <= 5; row++)

    {

        for (column = 1; column <= 3; column++)

        {

            cout << “*”;

        }

        // The content after this line is missing from the extraction

    }

    // There may be additional content here that is not included in the extraction.

}

cout << “\n”;

}

return 0;

}

Quiz

advanced divider