Sunday, October 30, 2011

The for Loop

Perhaps one reason why few programmers use while is that they are too busy using the for, which is probably the most popular looping instruction. The for allows us to specify three things about a loop in a single line: 

(a)  Setting a loop counter to an initial value.
(b)  Testing the loop counter to determine whether its value has reached the number of repetitions desired.
(c)  Increasing the value of loop counter each time the program segment within the loop has been executed.
The general form of for statement is as under:

for ( initialise  counter ; test  counter ; increment  counter )
{
  do this ;
      and this ; 
  and this ;
}

Let us write down the simple interest program using for. Compare this program with the one, which we wrote using while. The flowchart is also given below for a better understanding.



/* Calculation of simple interest for 3 sets of p, n and r */ 
main ( )
{
  int   p, n, count ;
  float   r, si ;
 
  for ( count = 1 ; count <= 3 ; count = count + 1 )
 {
    printf ( "Enter values of p, n, and r " ) ;
    scanf ( "%d %d %f", &p, &n, &r ) ;

    si = p * n * r / 100 ;
    printf ( "Simple Interest = Rs.%f\n", si ) ;
 }


If this program is compared with the one written using while, it can be seen that the three steps—initialization, testing and incrementation—required for the loop construct have now been incorporated in the for statement.
Let us now examine how the for statement gets executed:

  When the for statement is executed for the first time, the value of count is set to an initial value 1.

  Now the condition count <= 3 is tested. Since count is 1 the condition is satisfied and the body of the loop is executed for the first time.

−  Upon reaching the closing brace of for, control is sent back to the for statement, where the value of count gets incremented by 1.

−  Again the test is performed to check whether the new value of count exceeds 3.

−  If the value of count  is still within the range 1 to 3, the statements within the braces of for are executed again.

−  The body of the for loop continues to get executed till count doesn’t exceed the final value 3. 

−  When count reaches the value 4 the control exits from the loop and is transferred to the statement (if any) immediately after the body of for.

The following figure would help in further clarifying the concept of execution of the for loop.



It is important to note that the initialization, testing and incrementation part of a for loop can be replaced by any valid expression. Thus the following for loops are perfectly ok.

for ( i = 10 ; i ; i -- )
  printf ( "%d", i ) ;
for ( i < 4 ; j = 5 ; j = 0 )
  printf ( "%d", i ) ;
for ( i = 1; i <=10 ; printf ( "%d",i++ )
 ;
for ( scanf ( "%d", &i ) ; i <= 10 ; i++ )
  printf ( "%d", i ) ;

Let us now write down the program to print numbers from 1 to 10 in different ways. This time we would use a for loop instead of a while loop.

(a)  main( )
{
     int   i ;
     for ( i = 1 ; i <= 10 ; i = i + 1 )
           printf ( "%d\n", i ) ;
}

 Note that the initialisation, testing and incrementation of loop counter is done in the for statementitself.Instead of i = i + 1, the statements i++ or i += 1 can also be used.  Since there is only one statement in the body of the for loop, the pair of braces have been dropped. As with the while, the default scope of for is the immediately next statement after for.

(b)  main( )
{
     int   i ;
     for ( i = 1 ; i <= 10 ; )
 {
           printf ( "%d\n", i ) ;
           i = i + 1 ;
 }
}

 Here, the incrementation is done within the body of the for loop and not in the for statement. Note that inspite of this the semicolon after the condition is necessary. 

(c)  main( )
{
     int   i = 1 ;
     for ( ; i <= 10 ; i = i + 1 )
           printf ( "%d\n", i ) ;
}
Here the initialisation is done in the declaration statement itself, but still the semicolon before the condition is necessary.

(d)  main( )
{
     int   i = 1 ;
     for ( ; i <= 10 ; )
 {
           printf ( "%d\n", i ) ;
           i = i + 1 ;
 }
}
Here, neither the initialisation, nor the incrementation is done in the for statement, but still the two semicolons are necessary.

(e)  main( )
{
     int   i ;
     for ( i = 0 ; i++ < 10 ; )
           printf ( "%d\n", i ) ;
}

 Here, the comparison as well as the incrementation is done through the same statement, i++ < 10. Since the ++ operator comes after i firstly comparison is done, followed by incrementation. Note that it is necessary to initialize i to 0. 

(f)  main( )
{
     int   i ;
     for ( i = 0 ; ++i <= 10 ; )
           printf ( "%d\n", i ) ;
}

Here, both, the comparison and the incrementation is done through the same statement, ++i <= 10. Since ++ precedes i firstly incrementation is done, followed by comparison.
Note that it is necessary to initialize i to 0.

No comments:

Post a Comment