if (表示式) 語句 1 [ else 語句2 ]
// 遞回函數 power() 計算浮點數的整數次方 // ----------------------------------------- double power( double base, unsigned int exp ) { if ( exp == 0 ) return 1.0; else return base * power( base, exp-1 ); }
if ( n > 0 ) if ( n % 2 == 0 ) puts( "n is positive and even"); else // 與最後一個 if 匹配 puts( "n is positive and odd" );
if ( n > 0 ) { if ( n % 2 == 0 ) puts( "n is positive and even" ); } else // 與第一個 if 匹配 puts( "n is negative or zero" );
// 計算測量值的誤差 // -------------------------------- double spec = 10.0, measured = 10.3, diff; /* ... */ diff = measured - spec; if ( diff >= 0.0 && diff < 0.5 ) printf( "Upward deviation: %.2fn", diff ); else if ( diff < 0.0 && diff > -0.5 ) printf( "Downward deviation: %.2fn", diff ); else printf( "Deviation out of tolerance!n" );