C language several uses for loop

I. Introduction

  In practice, the C language, for circulation compared to the while loop and do-while loop is more flexible. The following brief summary of several uses for loop system.

Second, the specific usage

  The general form for loop:

    for (initialize; test; update)

      statement;

  initialize - initialization; test - test; update - update.

  1 - update expressions can be used down counter

  Example: for (secs = 5; secs> 0; --secs) {.......}

  2 - update expressions can make the counter at a faster rate increase

  Example: for (secs = 0; secs <60; secs + = 13) {.......}

  3 - can be used instead of the digital count characters

  Example: for (ch = 'a'; ch <= 'z'; ch ++) {.......}

  4 - In addition to testing iterations, you can also test other conditions

  示例: for (dried = 1; * dry dry dry * <= 600; dry ++) {} .......

  5 - can make incremental amount of geometric growth rather than arithmetic growth

  Example: for (secs = 10.0; secs <60.0; secs * = 1.3) {.......}

  6 - update expressions can use any valid expression

  示例:for (x = 1; y  <= 75; y = (++x * 5) + 50) {.......}

  [For the inner loop may be different variables, but this style is not very encouraged. ]

  7-- expression may omit one or more (but not semicolons shown), as long as the end of the cycle can comprise statements in the loop can be

  Example: for (n = 3; ans <= 25;) {ans * = n; .......}

  [If you omit the test expression test conditions it would have been determined to be true, that for loop will always execute it. ]

  8 - initialize expression is not necessarily the initial value to a variable, you can use printf () function, etc.

  示例:for (n = 1,printf ("The n is %d.\n", n); n  <= 5 ; n++) {ans *= n; .......}

  9 - loop behavior can be changed in the expression loop header

  Example: for (n = 1; n <= 25; n + = delta) {.......}

  Parameters delta value can be changed in the loop body. ]

Guess you like

Origin www.cnblogs.com/wyt123/p/10952214.html