Loops
In
most of your programs you will make use of loops to keep repeating a group of
statements for a fixed number of times.
For the process of repeating you need to make use of loops.
There are a number of loop statements.
For
loop statement :
Syntax is :
for
(initialization expression ;
test expression
; re-initialization expression)
{
statements to be repeated;
}
For loop is used for fixed number of iterations (or fixed number of times the loop is repeated).

Initialisation expression : This is executed only once when the loop first starts. It gives the loop variable an initial value.
Test expression : involves relational operators. It is executed each time before the body of the loop is executed. Only if the test expression is true (or satisfied) will the loop body be executed. If it is false then the control passes to the statement following the for loop.
Reinitialization expression : It is executed at the end of every loop after the loop body.
Example
:
//
TO FIND THE SQUARE OF NUMBERS FROM 1 TO 10
#include
<iostream.h>
int main( )
{
int var;
for (var = 1 ; var < = 10; var ++)
{
cout<<
“Square of ”<<var<< “is” <<var * var;
}
return 0;
}
In the above example, var is the loop variable. The initialization expression is var = 1. the test expression is var < = 10 and the re-initialization expression is var++ (i.e. incrementing the value of var after each loop).
Initially var is 1 and since 1 is less than 10, the body of the loop is executed. Then var is incremented by one. Now, the new var is 2. Again 2 is less than 10 and so the body is again executed with 2 as the var value.
This process is repeated until var becomes 10. Even when var is 10, since 10 is equal to 10, the loop body is executed. Then var becomes 11. But 11 is greater than 10 and hence the test expression is not true. So the compiler goes to the line after the loop. In this case, it is the end of the program.
While
loop statement :
While loop keeps on repeating the body of the loop as long as the loop condition is true.
The
syntax is :
while (test expression)
{
statements;
}
An
example :
//
TO FIND THE SQUARE OF A GIVEN NUMBER
#
include <iostream.h>
int main( )
{
char reply;
int num, square;
cout<< “Do you want to find the square of a number”;
cin>>reply;
while (reply = = ‘y’)
{
cout<< “Enter the number”;
cin>>num;
square = num*num;
cout<< “The square is” <<square;
cout<< “Do you want to square another number”;
cin>>reply;
}
return 0;
}
At the beginning, only if the user types ‘y’ will the program execute the while loop. At the end of the while loop, the program again gets a value for reply. Since, this is within the while loop, the program will again check whether reply is ‘y’. If it is then the program will execute the while body again. At the end of each loop the program gets the value of reply and checks whether the test condition is true. As long as the test condition is true, the while loop is executed.
The
difference between while loop and for loop is that, while loop does not have a
fixed number of iterations (or repetitions of the loop body. As long as
condition is true it keeps executing it).
The
next section deals with Decision Statements
or Go back to Contents
Copyright © 2002 Sethu Subramanian All rights reserved.