Looping Structure


  • The looping computational structures can be achieved through many control structures in ‘C++’ Looping is based on a condition.
  • A loop terminates only by asserting the status, say true or false of the condition. Loops can be classified as entry controlled and exit controlled loops based on the positioning of that condition.
  • There are three types of looping structure in C++.
    1. For loop
    2. While loop
    3. Do while lop
    
  • For & while loop are entry control loops, where as do while are exit control loop.

    For Loop

    SYNTAX:
       for(initialization; test condition; modifier expression)
        {
         body of the loop;
        }
    
  • Initialization: The control variable is initialized first with a simple assignment statement c=0;
  • Test condition: The value of the control variable is tested using this test condition. For each iteration this is performed. This is a relational expression, such as i < 10 which could determine the exit from the loop. If this relational expression is true then the loop is executed again, otherwise control exits.
  • Modifier expression: At the end of the loop the control variable is transferred back to the for statement. The control variable is modified, and the update value of the control variable is again tested. If the condition is satisfied, the loop continues, if not control exits.
  • You could also write a program to display all the numbers from 100 to 1, using a decrement (negative increment), as follows:
    Example:
    //Program to print the numbers from 100 to 1
    #include < iostream.h >
    void main()
    {
    cout << endl << "The first 100 natural numbers are " << endl; 
    for (int i=100;i > 0;i--)
    cout << i <<  ‘\t’;
    cout << endl;
    }
    
  • One of the interesting points of this loop is that it is concise. All the three operators - the initialization, the iterative test and the increment are stated in the for statement.

    Comma operator

  • When the execution of the loop depends on two variables or two conditions the for statement can be set up with two variables separated by a comma.
  • for (i=0,j=n;i<=n;i++,j--)
  • Note that the comma is included along with the usual semicolons. This is called the comma operator.
    Example:
    //Program to print ascending and descending numbers
    #include < iostream.h > 
    void main()
    {
    int n;
    cout << endl << “Enter a number ”<> n;
    cout << endl; 
    for (int i=0,j=n;i<=n;i++,j--)
    cout << k << ‘\t’<< j << endl;
    cout << endl;
    }
    

    Nesting of the for loop

  • Nesting of the for loop is permissible in C++, i.e. a body of a for loop can also have a for structure. To pictorially
    visualize this circumstance, look at the following figure,
    for (x=10;x >0;x--)	//outer for loop begins
    {
    for (y=1 ;y< =5;y++)	//inner for loop begins
    {
    .......
    }	//inner for loop begins
    .......
    }	//outer loop ends
    
  • In nested loops the inner loops are executed first and then the outer loops. The nesting is permissible to any depth. For better degree of readability the loops are to be indented properly.

    While loop

  • Looping on a condition can be achieved by the while construct of C++. This is the simplest of all control structures.
    SYNTAX:
    while (test condition)
    {
    body of the loop;
    }
    
  • This is an entry restricted loop
  • That is, only on the condition being true the body of the loop is executed. In other words, if the condition returns 0, the body of the loop is not executed even once.
  • This process of repeated execution, called iteration, continues until the test condition returns false.
  • On exiting from the loop, the control is transferred to the first statement outside the loop.
    Example

    Click here for fibonacci program

    do while Loop

  • An alternate to the while construct is the do control structure. This is also a decision dependent looping. But the difference here is that the condition is evaluated at the end, i.e. after executing the body of the loop once.
  • While basically was entry controlled. Therefore, the body of the loop may not be executed even once. There are occasions in programming, wherein the execution may have to be done at least once. This effect is achieved through the do statement.
    SYNTAX:
    do
    {
    body of the loop
    } while (condition);
    
  • On reaching the do statement, the control proceeds to execute the body of the loop. When the while statement is reached, the condition is evaluated. If the condition is true, control is transferred to the beginning of the do loop and the program continues to execute the body of the loop. Otherwise control is transferred out of the loop. This loop is an exit controlled loop because, the test condition is evaluated at the bottom of the do while loop.


Goto Statement << Previous

Next >> continue Statement

Our aim is to provide information to the knowledge seekers. 


comments powered by Disqus


Footer1