What is Repetition Structure in C++

- February 13, 2018
Repetition structure is used to execute a statement or group of statements repeatedly as long as the given condition remains true. In this structure, the statements are executed according to the given condition. If condition true (it returns 1) then set of statements are executed that are under the given condition. After this, control transfer to the top of the loop. This process continues until the given condition become false.
An algorithm with repetition structure is as follows:
Flowchart of C++ selection structure

Repetition statements are also called iteration structure or loops, that are used to repeat the same code mulitple times in succession.
In C++, there are three kinds of repetition structures:
Selection Structure Description
while loop It is used to execute a statement or group of statements as long as the given condition remains true.
do-while loop The "do-while" loop is similar to "while" loop but in this loop the body of loop comes before the test condition.
for loop It is used to execute a statement or set of statements repeatedly for a specified number of times. It is also known as counter-controlled loop.
Read: Relational Operators in C++

Example of Repetition Structure

The following source code of the program displays counting from 1 to 15 using selection (while loop) structure.

#include<iostream.h>

#include<canio.h>

main()

 {

   int num;

   num = 1;

   clrscr();

   while (num <= 15)

   {

      cout<<num<<endl;

      num++;

   }

   cout<<"Counting from 1 to 15";

   getch();

 }


Output of the program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Selection structure example 2 using "do-while" loop statement:
The following source code of the program displays six numbers from 1 to 6 and their cube by using selection (do-while) structure.

#include<iostream.h>

#include<canio.h>

main()

 {

   int num;

   clrscr();

   num = 1;

   cout<<"Number \t Cube\n";

   do

   {

      cout<<num<<'\t'<<num*num*num<<endl;

      num = num +1;

   } while (num<=6);

   getch();

 } //end main function


Output of the program:
Number      Cube
1                   1
2                   8
3                   27
4                   64
5                  125
6                   216

Selection structure example 3 using "for-loop" structure.
The following source code of the program displays the following series by using selection (for loop) structure:
1, 2, 4, 8, 16, 32, 64

#include<iostream.h>

#include<canio.h>

main()

 {

   int num, res;

   clrscr();

   for (num=1; num<=64; num+=num)

      cout<<num<<"\t";

   getch():

 }//end main function