if...else if...else Statement

  1. 流程圖
    \begin{tikzpicture}[node distance=1.5cm]\setlength \baselineskip{1ex}
\node [input,name=input] {};
......
  2. 語法
    if(boolean_expression 1)
    {
       /* Executes when the boolean expression 1 is true */
    }
    else if( boolean_expression 2)
    {
       /* Executes when the boolean expression 2 is true */
    }
    else if( boolean_expression 3)
    {
       /* Executes when the boolean expression 3 is true */
    }
    else 
    {
       /* executes when the none of the above condition is true */
    }
    
  3. 範例
    [dywang@deyu zzz]$ vim if4.c 
    [dywang@deyu zzz]$ cat if4.c 
    #include <stdio.h>
    
    int main() {
        int a;
    
        printf("Enter an integer: ");
        scanf("%d", &a);
    
        if ( a>10 ) {
    		printf("%d > 10\n", a);
    	}	
    	else if ( a<10) {
    		printf("%d < 10\n", a);
    	}	
        else {
    		printf("a = 10\n");
    	}
    }
    
  4. 編譯成功
    [dywang@deyu zzz]$ gcc -o if4 if4.c
    
  5. 執行結果一
    [dywang@deyu zzz]$ ./if4
    Enter an integer: 20
    20 > 10
    
  6. 執行結果二
    [dywang@deyu zzz]$ ./if4
    Enter an integer: 8
    8 < 10
    
  7. 執行結果三
    [dywang@deyu zzz]$ ./if4
    Enter an integer: 10
    a = 10