Monday, August 23, 2010

C for loop

The for loop uses three control expressions, separated by semicolons, to control the looping process.  The initialize expression is executed once, before any of the loop statements are executed.  If the test expression is true (or nonzero) the loop is cycled through once.  Then the update expression is evaluated, and the test expression is checked again.  The for loop is an entry-condition loop, which as noted earlier, means that the decision to go through one more pass of the loop is made before the loop is traversed  Thus, it is possible that the loop is never traversed.  The statement part of the form can be a simple statement or a compound statement.

Form

for(initialize; test; update)
statement

The loop is repeated until test becomes false, or zero.

example
1.  for (n = 0; n &lt 10; n++)
      printf("%d %d\n", n, 2*n+1);

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

     printf("     n   n cubed\n");
     for(num = 1; num&lt= 6; num++)
     printf("%5d %5d\n", num, num*num*num);


    }

0 comments: