top of page

The next set of control statements is loop statements. C++ has 3 loop statements viz for, while and do .. while
 

for statement 

for is the most compact loop statement . Here you initialize a variable, test it using a condition and then modify a it. Often for loop is used when the loop is to be repeated fixed number of times like iterating through an array.

 

Syntax

for(expr1;expr2;expr3)

   statement;

 

expr1 is initialization. expr2 is condition and expr3 is increment.  All these are optional.

 

To start with, expr1 is executed and variable is initialized and next expr2 is evaluated. If expr2 is true, the loop is repeated. If it is false, loop is terminated.

 

After each iteration, expr3 is executed - which modifies the control variable(often incrementing it).   After this, again expr2 is evaluated and loop is repeated as long expr2 is true. 

Now let us understand this with an example

The output of the program is
         8 16 24 32
if input is 8

In the program,

  • the variable i is defined and initialized with 1. 

  • Then it is compared with 5. Since condition i<5 is true, the cout statement is executed - which prints a*i = 8*1 = 8.

  • Next i++ increments i to 2.

  • i is compared with 5 and as condition i<5 is true, the cout is executed.

  • This loop is repeated till i becomes 5.


One nice thing about  for loop is, the control variable i can be defined within the for  statement. The life and scope of i will be  within the loop.

Let us look at another example - this time reading an array

  An array is a group of elements having a common name and identified by an integer index.

As we would like to process all the elements of the array, from 0th element to n-1 th element, for loop is very convenient.

Note the following things

  • arr is an array of integers.

  • In the for loop array elements are initialized.

  • In the second for loop, the elements of the array are added together into sum.

  • We also notice that, in the first loop, instead of one statement in the body of the loop, we use a block - which is enclosed in braces. We can use a block of statements  in all three types of loops .

Never ending loop?

Do you want to use an infinite loop?  A loop which continues forever?

 

For that, just write

 

for(;;)

  statement;

 

Here we are not initializing, not incrementing and nor testing condition. As the condition is empty, the loop never ends.

 

This example also shows us that the three expression in the for loop are optional. 

Exercises:

1. Write a program to read 10 elements of an integer array and print the sum of only positive integers.

2. Write a program to find the  largest element and smallest elements of an array.

3. Write a program to print the elements of an array in reverse order.

 
while loop  

A while loop is quite straight forward. It has a condition and body of the loop is  repeatedly executed  as long as this condition is true.

 

Syntax 

while(expression)

   statement;

 

For example

      int i = 0;

    while(i<=10)

          cout<<i;

 

To start with expression (condition - in this case i<=10) is evaluated.( Expression can be relational or even arithmetic. Remember that C and C++ use 0 as false and 1 as true.)

 

If the expression is true, statement (in this case cout<<i) is executed. Then again expression is evaluated. This is repeated as long as expression is true. Once it is false, loop is terminated. 

 

Let us see an example. 

We have re-written the ealier array example, using while loop for initializing array. Don't you agree that for loop is more suited for this example? :)
 

break or continue ?

 

There are some situations where we want to terminate the loop abruptly.

 

Some other times we may want to skip the current iteration and go to next iteration. For these purposes, we can  use break and continue statements within the body of a loop.

 

break : 

break will terminate the loop and take the control outside.( By loop we mean any of the three - for, while or do -while. )

Let us look at an example.

Here we are reading numbers from console infinitely - because we use while(true) . When -1 is entered, we terminate the loop because if m==-1, we execute break statement.

Output of this program will be

Enter a number (-1 to terminate)22 

Even Enter a number (-1 to terminate)7

Odd Enter a number (-1 to terminate)678

Even Enter a number (-1 to terminate)99

Odd Enter a number (-1 to terminate)-1

 

 

If there are nested loops, then  break will terminate the inner most loop.

do .. while loop

A do while loop is similar to a while loop, except  that the condition is tested at the end of loop. This ensures that the body of loop is executed at least once.  


A for and while loops are entry controlled loops, but a do ..while loop is exit controlled. 

Syntax
       do
        statement;
     while(expression);


Here the statement is executed. Then the expression is tested. If the expression is true, the loop is repeated.

Let us write an example.  

Here we read numbers and print if the number is even or odd as long as number entered is -1. As while(expression) is executed after the block, we are sure that the body of the loop gets executed at least once.
 

Exercises

  1. Write a program to print all even numbers from 0 to 100

  2. Write a program to read a number and print all its factors.

  3. Write a program to read a number n and find the sum of all the numbers from 1 to n. Can this program be written without using a loop?

  4. Write a program to read numbers and print their squares and cubes continuously until a 0 is entered.

  5. Write a program to read a number and determine whether it is a prime number.

bottom of page