next up previous contents
Next: 練習題 Up: 判斷 Previous: nested switch statements   Contents

運算子 ?:

  1. 語法:如果 Exp1 true 執行 Exp2,否則執行 Exp3,變數 var 等於執行結果。
    var=Exp1 ? Exp2 : Exp3;
    
  2. 範例
    #include <stdio.h>
     
    int main ()
    {
       /* local variable definition */
       int a = 100;
     
       /* check the boolean condition */
       ( a < 20 ) ? printf("a is less than 20\n" ) : printf("a is not less than 20\n" );
       printf("value of a is : %d\n", a);
     
       return 0;
    }
    



2015-01-16