Also know as looping statements, or loops. These statements execute the statement(s) repeatedly until condition remains true. Allows programmer to specify that a program shall repeat an action while some condition remains true. Types of repetition control statement includes

  • while statement
  • do while statement
  • for statement

While Statement

It executes the statement(s) repeatedly until condition remains true (Iteration). Statements contained in the while constitutes body of the while statement which can be a single statement or a block. As condition becomes false, loop is terminated and control is transferred to the statement after the repetition statement executes

Syntax:

initialization; 
while (loopContinuationCondition)
{
Statement;
Increment;
}

Example:

int product = 3;
while ( product <= 100 ) {
  product = 3 * product;
}

Assignment Exercise:

A class of ten students take a quiz. The grades ( integers in the range of 0 to 100) for this quiz are available to you. Calculates and display the total of all student grades and the class average on the quiz.

Counter control Repetitionis a technique in which a variable called counter is used to control iterations in a loop – counter controlled repetition. Also know as definite repetition. Iterations are known before loop is executed.  Counter-controlled repetition requires:

  • Name of the control variable (loop counter)
  • Initial value of the control variable
  • Loop continuation condition - Test perform in every iteration whether to continue loop or not
  • Increment or decrement by which control variable is modified each time through loop

C++ Example

int main(void)
{
int counter = 1; // Declare and initialize loop variable
while (counter {
cout&lt; counter++; // Incremental control variable by 1
} // End of while
getch();
return 0;
}

Another Example

int main(void)
{
int counter = 0; 	 // Declare and initialize loop variable
while (++counter <=10) {   // Loop continuation condition &
  // Increment
  cout<<counter<<"  ";
}
getch();
return 0;
}

For Statement

Another type of loop statement – for loop. It specifies the counter control repetition details in a single line of code. When the for statement begins execution, the control variable is declared and initialized. Then the loop – continuation condition is checked and if condition is true, body executes. At the end counter is incremented or decremented.

Syntax:

for ( initialization;  loopContinuationCondition;  Increment ) {
  Statement;
}
Initialization expression initializes loop control variable
LoopContinuationCondition determines whether loop should continue or not
Increment  modifies control variable value

Example:

int main(void)
{
for (int counter = 1; counter <=10; counter++ ){
   cout<<counter<<"  ";
}
getch();
return 0;
}

Few Examples Using “for” statement

1. Vary the control variable from 1 to 100 increment by 1
 for ( int i = 1;  i <=100;  i++ )

2. Vary the control variable from 100 down to 1 in the increment of  -1

 for ( int i = 100;  i >=1;  i-- )
3. Vary the control variable from 7 to 77 in steps of  7
 for ( int i = 7;  i <=77;  i+=7 )
4. Vary the control variable from 20 down to 2 in the steps of -2
  for ( int i = 20;  i >=2;  i -=2 )
5. Vary the control variable over the following sequence of values: 2, 5, 8, 11, 14, 17
  for ( int i = 2;  i <=20;  i += 3 )
6. Vary the control variable over the following sequence of values: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0
  for ( int i = 99;  i >=0;  i -= 11 )

 

do…while Statement

This control statement is similar to the while statement. In the while statement, the loop continuation condition is checked at the beginning of the loop before the body of loop executed. The do…while statement tests the loop-continuation condition after the loop body executes. Therefore the loop body always executes at least once.

When the do…while terminates, execution continues with the statement after the while clause. Note that it is not necessary to use braces in the do…while  if there is only one statement in the body. However most programmer include the braces to avoid confusion between the while and do…while statements.

We can use a do…while statement to implement the data-validation loop -  a loop that continues to prompt and read data until valid data item is entered.

Syntax:

initialization; 
do
{
  Statement;
  Increment;
}
while (loopContinuationCondition);

Example:

int main(void)
{
int counter = 1;
do
{
  cout<<counter<<"  ";
  counter++;
}
while (counter <=10);
getch();
return 0;
}

Review of repetition control statements

while loop

  • Mostly used when repetition is not counter controlled
  • Condition test precedes each loop repetition
  • Loop body may not be executed at all
for loop
  • Used to implement a counting loop
  • Also convenient for other loops with simple initialization and update steps
  • Condition test precedes the execution of the loop body
do..while loop
  • Convenient when at least one repetition of loop body is needed

Related Posts

Tagged with: C/C++ language
 

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

Looking for something?

Use the form below to search the site:


Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds