if() ... else 語句和語法
雖然 if 僅在其條件評估為 true 時執行動作,但 if / else 允許你在條件 true 和條件 false 時指定不同的動作。
例:
if (a > 1)
puts("a is larger than 1");
else
puts("a is not larger than 1");
就像 if 語句一樣,當 if 或 else 中的塊只包含一個語句時,可以省略大括號(但不建議這樣做,因為它很容易引起問題)。但是,如果 if 或 else 塊中有多個語句,則必須在該特定塊上使用大括號。
if (a > 1)
{
puts("a is larger than 1");
a--;
}
else
{
puts("a is not larger than 1");
a++;
}