if() ... else Ladder Chaining 兩個或更多 if() ... else 語句
雖然 if ()... else
語句允許僅定義在 if ()
中的條件不滿足時發生的一個(預設)行為,但連結兩個或更多 if () ... else
語句允許在去往最後一個 else
分支之前定義更多行為作為預設 “,如果有的話。
例:
int a = ... /* initialise to some value. */
if (a >= 1)
{
printf("a is greater than or equals 1.\n");
}
else if (a == 0) //we already know that a is smaller than 1
{
printf("a equals 0.\n");
}
else /* a is smaller than 1 and not equals 0, hence: */
{
printf("a is negative.\n");
}