The For Loop
The "for" loop is really nothing new, it is simply a new way of describe the "while" loop. Load
and edit the file named forloop.c for an example of a program with a "for" loop.
Syntax: for ( [<expr1>] ; [<expr2>] ; [<expr3>] ) <statement>
<statement> is executed repeatedly UNTIL the value of <expr2> is 0.
BEFORE the first iteration, <expr1> is evaluated. This is usually used to
initialize variables for the loop.
AFTER each iteration of the loop, <expr3> is evaluated. This is usually used
to increment a loop counter.
Program
main( )
{
int index;
for(index = 0;index < 6;index = index + 1)
printf("The value of the index is %d\n",index);
}
when the above program first check condition and then loop will continued until the condition will false
when above program the
index=0 is initialization of the loop
index<6 is condition of loop
index+1 is the increment of loop
when above program executed until the index value can be 7 and more the program o/p
is
output: The value of the index is 0
The value of the index is 1
The value of the index is 2
The value of the index is 3
The value of the index is 4
The value of the index is 5
The value of the index is 1
The value of the index is 2
The value of the index is 3
The value of the index is 4
The value of the index is 5
0 comments:
Post a Comment