next up previous contents
Next: do...while loop Up: 迴圈 Previous: while loop   Contents

for loop

  1. 流程圖

    \begin{tikzpicture}[node distance=1.5cm]\setlength \baselineskip{1ex}
\node [input,name=input] {};
\node [block, below of=input] (init) {init};
\node [decision, below of=init] (decision) {condition?};
\node [block, below of=decision] (code) {block code};
\node [block, below of=code] (increment) {increment};
\node [cir,below of=increment] (sum) {};
\node [output,below of=sum,name=output] {};
\path [line] (input) --(init);
\path [line] (init) --(decision);
\path [line] (decision) -- node[right]{true}(code);
\path [line] (decision.east) -\vert node[right] {false} ($(decision.east) + (1.5,0)$) \vert- (sum.east);
\path [line] (code) --(increment);
\path [line] (increment) \vert- node[]{} ($(increment.west)+(-1.5,-1)$) \vert-(decision.west);
\path [line] (sum) -- (output);
\end{tikzpicture}

  2. 語法
    for ( init; condition; increment )
    {
       statement(s);
    }
    
  3. 範例
    #include <stdio.h>
     
    int main ()
    {
        int a,sum=0;
       /* for loop execution */
       for( a=10; a<20; a++ )
       {
        sum+=a;
       }
       printf("value of a: %d\n", sum);
     
       return 0;
    }
    



2015-01-16