賦值:三目運算符實現的條件判斷:條件成立賦左值,條件不成立賦右值code
int a=10, b=20, z; z = (a>b) ? a : b;//條件成立賦左值z=a,條件不成立賦右值z=b =>z=2
選擇語句執行:條件成立左側語句執行,不成立右側語句執行class
int i = 1, j = 2, k = 3; i == 0 ? (i = j + k, j = 5) : (k++, k++); cout << i << ' ' << j << ' ' << k << endl; 輸出 1 2 5
int i = 1, j = 2, k = 3; i == 1 ? (i = j + k, j = 5) : (k++, k++); cout << i << ' ' << j << ' ' << k << endl; 輸出 5 5 3