[<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
for                      for Statement

 for ( [initialization] ; [termination-condition] ; [statement] )

    The for loop causes its body of statements to be executed provided
    `termination-condition' is true. If `termination-condition' is false
    at the start of the loop, the loop's body is never executed. Prior to
    testing `termination-condition' for the first time, `initialization'
    is evaluated.  At the end of execution of the loop body, `statement'
    is evaluated and then `termination-condition' is tested.

      Notes:    All three expressions are optional. The construct:

                        for (;;)

                has no initialization, no termination criteria and
                nothing to evaluate at the end of the loop.

                The termination condition is tested before the body is
                executed the first time.  If it is initially FALSE, the
                body is never executed.  Use do-while when you want the
                body executed at least once.

                Remember that you can use the comma operator to squeeze
                multiple operations into a single statement; see the
                first example below, where the comma operator is used to
                increment two variables.

  -------------------------------- Example ---------------------------------

           for (i = 0; i < length; i++, j++)
               new_name[i] = toupper(old_name[j]);

           for (i = 0, j = 0, k = 0; i < max; ) {
               testval(i, j, k);
               j = i * i;
               k += 2;

           for (i = 0, j = 0, k = 0; i < max; ++i, j = i * i, k += 2)
               testval(i, j, k);

See Also: break return goto while

Online resources provided by: --- NG 2 HTML conversion by Dave Pearson