next up previous contents
Next: for loop Up: 迴圈 Previous: 迴圈   Contents

while loop

  1. 流程圖

    \begin{tikzpicture}[node distance=1.5cm,auto]\setlength \baselineskip{1ex}
\node [input,name=input] {};
\node [decision, below of=input] (decision) {condition?};
\node [block, below of=decision] (code) {block code};
\node [cir,below of=code] (sum) {};
\node [output,below of=sum,name=output] {};
\path [line] (input) --(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) \vert- node[]{} ($(code.west)+(-1.5,-1)$) \vert-(decision.west);
\path [line] (sum) -- (output);
\end{tikzpicture}

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



2015-01-16