The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as
well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. Donald E. Knuth

C++ Loops

Loop control statements are used to execute a statement or a group of statements multiple times. Suppose you want to print a number 10 times. Instead of using 10 cout statements, we can simply loop over a single cout statement 10 times. Let's see the looping constructs supported by C++.

' for ' Loop
Consider the following code snippet :

for ( int i = 1; i <= 10; i++ ) {
      cout << " i = " << i ;
}

i = 1; is the initialization statement.
i <= 10; is the condition statement.
i++; is the increment statement.

The initialization ( i = 1; ) statement is executed once.
Then, the condition ( i <= 10; ) statement is checked. If it evaluates to True, body of the loop is executed. If it evaluates to False, loop is terminated and the statement after the loop body is executed.
After all the statements in loop body are executed, control jumps to the increment statement and the condition statement is evaluated again.
Every for loop follows this syntax :

for ( initialize; condition; increment / decrement ) {
      statement 1;
      statement 2;
              .
              .
      statement n;
}

for loop is known as entry controlled loop since the condition check is done at beginning of the loop. Following program illustrates the use of for loop :

/*
 * This program prints the multiplication table of a given number
 * using for loop
 */

#include<iostream>
using namespace std;

int main() {
   int num = 7, value, i;
   cout <<"Multiplication Table for " << num << " :- " << endl;
   // following loop executes 10 times
   for ( i = 1; i <= 10; i++ ) {
      value = num * i;
      cout << num << " * " << i << " = " << value <<endl;
   }
   return 0;
}

' while ' loop
Syntax of the while loop is :

while ( condition ) {
      statement 1;
      statement 2;
              .
              .
      statement n;
}

Initialization statement is declared just before the while statement.
Increment / Decrement statement is present inside the body of while loop.

while loop is also an entry controlled loop. It is possible that the body of the while loop is not executed even once. Following program illustrates the use of while loop :

/*
 * This program prints the multiplication table of a given number
 * using while loop
 */

#include<iostream>
using namespace std;

int main() {
   int num = 6, value, i;
   cout <<"Multiplication Table for " << num << " :- " << endl;
   i = 1; // initialization
   while ( i <= 10 ) { // condition
      value = num * i;
      cout << num << " * " << i << " = " << value <<endl;
      i++;
   }
   return 0;
}

' do ... while ' loop
Syntax of the do ... while while loop is :

do {
      statement 1;
      statement 2;
              .
              .
      statement n;
} while ( condition );

' do ... while ' loop is know as exit controlled loop since the condition check is done at the end of the loop. Following program illustrates the use of do ... while loop :

/*
 * This program prints the multiplication table of a given number
 * using do ... while loop
 */

#include<iostream>
using namespace std;

int main() {
   int num = 5, value, i;
   cout <<"Multiplication Table for " << num << " :- " << endl;
   i = 1; // initialization
   do {
      value = num * i;
      cout << num << " * " << i << " = " << value <<endl;
      i++;
   } while ( i <= 10 ); // condition check for exit   
   return 0;
}

' goto ' statement
' goto ' statement is used to break the current execution flow of a program and jump to a specified label in a program. This can be used as another technique to implement loops. However it is recommended to use for, while, do ... while loops only. Following program illustrates the use of ' goto ' statement :

/*
 * This program prints the multiplication table of a given number
 * using goto statement
 */

#include<iostream>
using namespace std;

int main() {
   int num = 8, value, i;
   cout <<"Multiplication Table for " << num << " :- " << endl;
   i = 1; // initialization
   LOOP : // Label
      value = num * i;
      cout << num << " * " << i << " = " << value <<endl;
      i++;
      if ( i <= 10 ) { // condition check
         goto LOOP;
      }
   // end of loop    
   return 0;
}

Nested Loops
We can use a loop inside the body of another loop. This is known as nesting of loops. Following program illustrates the use of nested loops:

/*
 * This program computes the average marks for 'n' students.
 * No. of students = n is taken from user.
 * For each student, marks of 3 subjects is taken as input and 
 * average marks is computed using ( marks1 + marks2 + marks3 ) / 3 
 */

#include<iostream>
using namespace std;

int main() {
   int n; // no. of students
   int i, j; // loop iterators
   float marks; // marks of subjects is stored here
   float sum, avg;
   cout << "Enter the no. of students : ";
   cin >> n;

   // iterate over the outer loop for 'n' times
   for ( i = 0; i < n; i++ ) {
      sum = 0.0;
      cout << "Enter marks of student " << i+1 << " :- " << endl;
      // iterate over the inner loop 3 times for each student
      for ( j = 0; j < 3; j++ ) {
         cout << "Subject " << j+1 << " : ";
         cin >> marks;
         sum += marks;
      }
      avg = sum / 3; // average of marks of 3 subjects
      cout << "Average marks : " << avg << endl;
   }
   return 0;
}
Run this program in your system to take input at run-time  

' break ' and ' continue ' statement
' break ' statement is used to terminate the loop immediately without going for further conditional check. In case of nested loop, the innermost loop is terminated and the execution starts from the next line of the program after the inner loop. We have already seen that break is used to break out of switch case construct.
' continue ' statement doesn't terminate the loop but goes to the beginning of the loop for next iteration skipping rest of the loop body.
Following program illustrates the use of break statement :

/*
 * Program to demonstrate the behaviour of break statement
 */

#include<iostream>
using namespace std;

int main() {
   int i;
   for ( i = 0; i < 10; i++ ) {
      if ( i == 7 ) {
         // loop terminates here
         break;
      }
      cout << i << " ";
   }
   return 0;
}

The for loop is supposed to have 10 iterations printing 0 1 2 3 4 5 6 7 8 9 but the break statement causes termination of the loop once i becomes 7. So the output will be 0 1 2 3 4 5 6 .

Following program illustrates the use of continue statement :

/*
 * Program to demonstrate the behaviour of continue statement
 * This program prints odd numbers only from 0 to 9
 */

#include<iostream>
using namespace std;

int main() {
   int i;
   for ( i = 0; i < 10; i++ ) {
      if ( i % 2 == 0 ) { // check for even number
         // 'i' is even, goto the beginning of the loop
         // skip the print statement 
         continue;
      }
      cout << i << " ";
   }
   return 0;
}

Output of the above program is 1 3 5 7 9 . The print statement is skipped using the continue statement when we see an even number.

Back | Next