Looping Structure in C++

Looping Structure

Looping structure allows to execute a statement or group of statements multiple times.

It provides the following types of loops to handle the looping requirements:

1. While Loop
2. For Loop
3. Do . . . While Loop

1. While Loop

  • While loop allows to repeatedly run the same block of code, until a given condition becomes true.
  • It is called an entry-controlled loop statement and used for repetitive execution of the statements.
  • The loop iterates while the condition is true. If the condition becomes false, the program control passes to the next line of the code.
Flow Diagram of While Loop
while loop diagram
Syntax:
while (Condition)
{
     //Statements;
}

Example : Demonstrating execution of While loop

Fibonacci series program to demonstrate execution of While loop.

#include <iostream>
using namespace std;
int main()
{
      int num1 = 0, num2 = 1, num3 = 0;
      cout<<"Fibonacci Series:"<<endl;
      while(num2 <= 10)
      {
            num3 = num1 + num2;
            num1 = num2;
            num2 = num3;
            cout<<num3<<endl;
      }
      return 0;
}


Output:
Fibonacci Series:
1
2
3
5
8
13

2. For Loop

  • For loop is a compact form of looping.
  • It is used to execute some statements repetitively for a fixed number of times.
  • It is also called as entry-controlled loop.
  • It is similar to while loop with only difference that it continues to process block of code until a given condition becomes false and it is defined in a single line.

  • It includes three important parts:

    1. Loop Initialization
    2. Test Condition
    3. Iteration

  • All the above parts come in a single line separated by semicolon (;).
Flow Diagram of For Loop
flow diagram for loop
Syntax:
for (initialization; test-condition; increment/decrement)
{
    //Statements;
}

Example : Demonstrating the execution of For Loop

#include <iostream>
using namespace std;
int main()
{
    int i = 1, j = 5;
    for(i=1; i<=j; i++)
    {
        cout<<"For Loop Execution"<<i<<endl;
    }
    return 0;
}


Output:
For Loop Execution:1
For Loop Execution:2
For Loop Execution:3
For Loop Execution:4
For Loop Execution:5

3. Do . . . While Loop

  • Do . . . While loop is executed at least once, even if the condition is false.
  • It is called as an exit-controlled loop statement.
  • This loop does not test the condition before going into the loop. The condition will be checked after the execution of the body that means at the time of exit.
  • It is guaranteed to execute the program at least one time.
Flow Diagram of Do . . . While Loop
flow diagram do while loop
Syntax:
do
{
     //Statements;
}
while(Condition);

In Do-while loop, while condition should always have a semi-colon at the end.

Example : Demonstrating the execution of Do-While loop

#include <iostream>
using namespace std;
int main()
{
     int num=0;
     do
     {
          cout<<“Do While Loop Execution:”<<num<<endl;
          num++;
     }
     while(num<=5);
     return 0;
}


Output:
Do While Loop Execution:0
Do While Loop Execution:1
Do While Loop Execution:2
Do While Loop Execution:3
Do While Loop Execution:4
Do While Loop Execution:5