if...else statement

  1. 流程圖
    \begin{tikzpicture}[node distance=1.5cm]\setlength \baselineskip{1ex}
\node [input,name=input] {};
......
  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. 範例
    [dywang@deyu zzz]$ vim if2.c 
    [dywang@deyu zzz]$ cat if2.c
    #include <stdio.h>
    
    int main() {
        int a;
    
        printf("Enter an integer: ");
        scanf("%d", &a);
    
        if ( a>10 ) {
    		printf("%d > 10\n", a);
    	}
        else {
    		printf("%d <= 10\n", a);
    	}
        printf("a = %d\n", a );
    }
    
  4. 編譯成功
    [dywang@deyu zzz]$ gcc -o if2 if2.c
    
  5. 執行結果一
    [dywang@deyu zzz]$ ./if2
    Enter an integer: 7 
    7 <= 10
    a = 7
    
  6. 執行結果二
    [dywang@deyu zzz]$ ./if2
    Enter an integer: 19
    19 > 10
    a = 19