The for loop is used for repetitive execution of a statement or a group of statements. It is a very powerful and flexible statement of the C language. It is generally used in situations where the number of iterations of the loop statement is known or can be determined in advance.
It can also be used in situations where the number of iterations is not known or cannot be determined in advance. However, the while loop is more convenient in such situations. The general form of the for loop is given below.
for ( initial_ expr ; final_ expr ; update_ expr )
statement;
This loop executes the contained statement repeatedly as decided by three control expressions (initial_expr, final_expr and update_expr). The initial_expr is usually an assignment expression, whereas the update_expr is an increment/decrement or compound assignment expression. The final_expr expression is usually a relational expression but may also be a Boolean expression or any valid C expression. Observe that the control expressions are written within a pair of parentheses and are separated by semicolons.
The most commonly used form of the for loop uses a loop control variable (also called loop variable, counter variable or index variable). It is a variable of any arithmetic type such as integer, floating-point, character, etc. Most programmers prefer short names for loop variables, e. g., variables i, j and k are commonly used in situations where integer loop variables are required.
The loop variable assumes a sequence of values as decided by the three control expressions. For each value of the loop variable, the statement included in the for loop is executed once. The initial_ expr initializes the loop variable, the update_expr updates (increment, decrement, etc.) its value and the final_expr is a test expression that compares its value with the desired final value to decide whether the execution of the contained statement should be continued or not.
Consider the for loop given below used to print the string “Hello world\n” four times.
for (i = l; i <= 4; i++)
printf(“Hello world\n”);
In this for loop, variable i (assumed to be of type int) is used as a loop variable. The initial expression, i = 1 , initializes the i to 1. The final expression, i <= 4 , compares value of i with the desired final value, i.e., 4. The update expression, i++, increments the value of i by 1. Thus, the loop variable i assumes values 1, 2, 3 and 4. For each of these values, the printf statement prints the string “Hello world” followed by a newline. Thus, the output is as follows:
Hello world
Hello world
Hello world
To understand the execution of the for loop, refer control flow diagram shown in Fig and the flowcharts in Fig. The execution of the for loop proceeds as follows: Initially, expression initial_ expr is evaluated. It performs the initialization of the loop variable. Then final_expr is evaluated. If it is true, the statement included in the loop is executed followed by evaluation of update_expr.
Then final expr is evaluated again. The execution of statement within the loop followed bythe evaluation of update_expr and final_expr continues as long as final_expr evaluates true; otherwise, the control leaves the for loop and continues with the execution of the next statement in the program. Note that the execution of the contained statement followed by the evaluation of update expression and final expression is commonly referred as an iteration of the loop. Also, the statement contained in the loop is commonly referred to as the body of the loop. ·
Although the values of loop variables used in the above for loop are quite natural, it is a common practice to use zero as the initial value for the loop variable. Moreover, as the array subscripts start from zero, the for loops used to process arrays also use zero as the initial value of loop variable. Hence, the style of using zero as the initial value of loop variables, wherever appropriate, is strongly recommended. Such a for loop to perform n iterations is given below.
for (i = 0; i < n; i++)
Note the use of the ‘<‘ operator in the final expression (i < n). Now the program segment to print Hello world five times can be rewritten as follows: ·
for (i = 0; i < 5; i++)
printf(“Hello world\n”);
Illustrates the application of a for loop for calculating factorial of a given number
#include<stdio.h>
void main()
{
int i, n , Factorial=1;
clrscr();
printf("Enter the number for finding factorial.");
scanf("%d", &n);
printf("n\tFactorial\n" );
for ( i= 1;i<=n; i++)
Factorial *= i;
printf( "%d\t%d\n", n, Factorial);
}
In one execution of the above program, the number entered is 5 and value of 5! is given by the program.
In the above program you would observe that the loop starts from the line for (int i=1; i<=n; i++) and ends at the statement Factorial *=i;. For the next iteration, the i is incremented, tested by the middle expression of the for loop and the iteration is carried out if the test is not false. So, in every iteration, the value of i is multiplied to the previous value of the factorial. At the end of the loop, we obtain Factorial= 1×2 x 3 x 4 x 5 =120.