next up previous contents
Next: if...else statement Up: 判斷 Previous: 判斷   Contents

if statement

  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) {condition code};
\node [cir,below of=code] (sum) {};
\node [output,below of=sum] (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) -- (sum);
\path [line] (sum) -- (output);
\end{tikzpicture}

  2. 語法
    if(boolean_expression)
    {
       /* statement(s) will execute if the boolean expression is true */
    }
    
  3. 範例
    #include <stdio.h>
     
    int main ()
    {
       /* local variable definition */
       int a = 10;
     
       /* check the boolean condition using if statement */
       if( a < 20 )
       {
           /* if condition is true then print the following */
           printf("a is less than 20\n" );
       }
       printf("value of a is : %d\n", a);
     
       return 0;
    }
    



2015-01-16