Saturday, December 19, 2024

Loop Contro in C : for loop


Loop Control in C : for loop





Syntax :

                 for(expression1 ; expression2 ; expression3)             
                  {
                             body;
                  }



expression1
Initialization
expression2
Test Condition
expression3
Update Expression

Execution :
  1. Expression1 is evaluated only once at the beginning .
  2. Expression2 is test Condition which is executed before executing body of the loop,if TEST CONDITION is true then only the Body of Loop is Executed.
  3. Expression3 is update Expression.It alters the value of Loop Control Variable


Example :
                         for ( i=0 ; i<100 ; i++ )
                         {
                          printf("Hi");

                         }



i
Subcript/Counter/Loop Control  Variable
i=0
Initialization
i<100

Test Condition
i++
Update Expression

Result :
  • It will Print the "Hi" 100 times
Steps
  1. Refer to Topmost fig. Initially i = 0 initializes Loop Control Variable.
  2. It is checks with i<100 condition .
  3. Condition is found to be True. So it will Execute the Body of Loop.
  4. After execution , It will Execute Expression3 ( Update ) Now i = 1;
  5. Condition is again True so again body of loop is executed ,
  6. Similarly Body of loop is executed 100 times.
  7. At the end i = 100 & i<100 Condition violets So it bypasses the Loop body






    Bookmark & Share

    1 comments:

    Abdus said...

    very very good.the method is mind blowing.

    Post a Comment

    Your Feedback :This is Growing Site and Your Feedback is important for Us to improve the Site performance & Quality of Content.Feel Free to contact and Please Provide Name & Contact Email

    Copyright Notice | Link Exchange | Privacy Policy |Contact Us

     
    Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | cheap international calls