for loop


for loop  C programing :



The for loop is another entry-controlled loop that provides a more concise loop control structure .the general form of the for loop is
for(intialization ; tes -condition ; increment / decrement )
{
      body of the loop
}

   The execution of for statement is as followes :

  1. Initalization of the control variables is done first , using assigenment statements such as i =1  and count =0.  The vaiables i and count are known as loop -contrl variables .
  2. The  value of the  control variable is tested using the test-condition . The test-condition is  a relational  expression , surch as i < 10  that determines when the loop will wxit . If  the condition is ture , the body  of the loop is executed ; otherwise the loop is terminated and the execution continues with  the statement the 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 if the control variabel is again tested  to see whether it satisfies the loop  condition . If the condition is satisfied , the body of the loop is again executed . This process continues till the value of the control variable fails to satisfy the test-condition .  



(   In real life we come across situations when we need to perform a set of task repeatedly till some condition is met. Such as – sending email to all employees, deleting all files, printing 1000 pages of a document. All of these tasks are performed in loop. To do such task C supports looping comtrol statement.
For loop is an entry controlled looping statement. It is used to repeat set of statements until some condition is met.
Looping statements whose condition is checked prior to the execution of its body is called as Entry controlled loop.)



Flowchart of for  loop :








example c program for loop :







Let us  write a C program that print natural numbers from 10 to 20



            #include<stdio.h>
          int main()
          {

           /* Declare loop i variable */
           int i;

            /* Run a loop from 10 to 20 */
            for(i=10;i<=20;i++){

            /* Print current value of i */
            printf("%d ",i);
        }
}



output :

        10 11 12 13 14 15 16 17 18 19 20




No comments

Powered by Blogger.