next up previous contents
Next: nested if statements Up: 判斷 Previous: if statement   Contents

if...else statement

  1. 流程圖

    \begin{tikzpicture}[node distance=1.5cm]\setlength \baselineskip{1ex}
\node [input,name=input] {};
\node [decision, below of=input] (decision) {condition?};
\node [block, below of=decision] (truecode) {ture condition code};
\node [block, right of=truecode, node distance=4cm] (falsecode) {false condition code};
\node [cir,below of=truecode] (sum) {};
\node [output,below of=sum] (output) {};
\path [line] (input) --(decision);
\path [line] (decision) -- node[right]{true}(truecode);
\path [line] (decision.east) -\vert node[right] {false} (falsecode.north);
\path [line] (falsecode.south) \vert- (sum.east);
\path [line] (truecode) -- (sum);
\path [line] (sum) -- (output);
\end{tikzpicture}

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



2015-01-16