THE FOR  STATEMENT


THE FOR  STATEMENT
The for loop is another entry-controlled loop that provides a more concise loop control structure. The general form of the for loop is shown below:
for(initialization; test-condition; increment)
{
body of the loop
}
The execution of the for statement is as follows:

1. Initialization of the control variable is done first, using assignment statements such as i=1. The variable i is  known as loop-control variable.

2. The value of the control variable is tested using the test-condition. The test condition is a relational expression, such as i<10 that determines when the loop will exit. If the condition is true, the body of the loop is executed; otherwise the loop is terminated and the execution continues with the statement that immediately follows the loop.

3. When the body of the loop is executed, the control is transferred back to the for statement after evaluating the last statement in the loop. Now, the control variable is incremented using an assignment statement such as i = i+1 and the new value of the control variable is again tested to see whether it saatisfies the loop condition. If the conition is satisfied, the body of the looop is again executed. This process continues till the value of the control variable fails to satisfy the test-condition.

#include<stdio.h>
void main()
{
int  x;
clrscr();
for(x = 0; x <= 9; x = x + 1)
{
printf(“%d”, x);
}
printf(“\n”);
getch();
}

Comments