Introduction to C / C++ Programming
Looping Constructs

Computers are very good at performing repetitive tasks very quickly. In this section we will learn how to make computer repeat actions either a specified number of times or until some stopping condition is met.

while Loops ( Condition-Controlled Loops )

        while( condition ) 
            body;

where the body can be either a single statement or a block of statements within { curly braces }.

        int i = 0;
        while( i < 5 ) 
            cout << "i = " << i++ << endl;
        cout << "After loop, i = " << i << endl;

do-while Loops

        do { 
            body;
        } while( condition );
        int month;
        do { 
            cout << "Please enter the month of your birth > ";
            cin >> month;
        } while ( month < 1 || month > 12 );

for Loops

  
          

where again the body can be either a single statement or a block of statements within { curly braces }.

        
            for( int i = 0; i < 5; i++ )
                cout << "i = " << i << endl;
            cout << "After loop, i is no longer valid\n";

break and continue

Infinite Loops

        int i, j;
        for( i = 0; i < 5; j++ )
            cout << "i = " << i << endl;
        cout << "This line will never execute\n";
        while( true ) {
            cout << "Please enter a month from 1 to 12 > ";
            cin >> month;
            if( month > 0 && month < 13 )
                break;
            cout << "That is not a valid month.  Please try again.\n";
        }

Empty Loops

        int i;
        for( i = 0; i < 5; i++ ); // Error on this line causes empty loop
            cout << "i = " << i << endl;  // This line is AFTER the loop, not inside it.
        int i = 0;
        while( i < 5 );  // Error - empty loop on this line
            cout << "i = " << i++ << endl; // Again, this line is AFTER the loop.

Nested Loops

        const int NROWS = 10;
        const int NCOLS = 10;
		
        for( int r = 0; r < NROWS; r++ ) { // Loop through rows
            for( int c = 0; c < NCOLS; c++ ) { // Loop through columns

                cout << setw( 5 ) <<  r * c ;

            } // End of loop through columns

            cout << endl;

        } // End of loop through rows

Floating Point Increments Within Loops

        for( time = tMin; time <= tMax; time += deltaT ) {
            // Use the time variable in the loop
        int nTimes = ( tMax - tMin ) / deltaT + 1;
        for( int i = 0; i < nTimes; i++ ) {
            time = tMin + i * deltaT;
             // NOW use a more accurate time variable
        double deltaT = ( tMax - tMin ) / ( nTimes - 1 );
        for( int i = 0; i < nTimes; i++ ) {
            time = tMin + i * deltaT;
             // NOW use a more accurate time variable

When to Use Which Loop ?

Exercises

  1. Write a program that generates a table showing the difference between "time1" calculated as "time1 += deltaT" and "time2" calculated as "time2 = i * deltaT", for iterations from 0 up to an upper limit specified by the user. The user should also specify a frequency for how frequently (s)he wants the results printed in the table. So if they enter 10 for the frequency you should print results every 10th time through the loop, and if they enter 100, then print every 100th time through the loop, etc. Hint: Use mod for the printing frequency.
  2. Write a program to calculate epsilon, the smallest value that can be added to 1.0 and still be distinguished from 1.0. Hint: Start with epsilon equal to 1.0, and then keep dividing it by 2.0 until 1.0 + epsilon / 2.0 is indistinguishable from 1.0;