C - if-else 語句



if-else 語句是 C 語言中經常使用的決策語句之一。if-else 語句在條件不滿足時提供了一條備選路徑。

else 關鍵字幫助您提供當 if 語句中的布林表示式結果為假時要採取的備選操作方案。else 關鍵字的使用是可選的;您可以選擇是否使用它。

if-else 語句的語法

以下是 if-else 子句的語法:

if (Boolean expr){
   Expression;
   . . .
}
else{
   Expression;
   . . .
}

C 編譯器 會評估條件,如果條件為真,則執行 if 語句後面的語句或語句塊。

如果程式設計邏輯需要計算機在條件為假時執行其他指令,則將其作為 else 子句的一部分。

if 語句後面可以跟一個可選的 else 語句,當布林表示式為假時執行該語句。

if-else 語句的流程圖

以下流程圖表示 if-else 子句在 C 語言中的工作方式:

C if...else statement

請注意,如果 ifelse 子句中有多個語句,則需要花括號。例如,在以下程式碼中,我們不需要花括號。

if (marks<50)
   printf("Result: Fail\n");
else
   printf("Result: Pass\n");

但是,當 ifelse 部分有多個語句時,您需要告訴編譯器將它們視為複合語句。

C if-else 語句示例

示例:使用 if-else 語句計算稅款

在下面給出的程式碼中,計算了員工收入的稅款。如果收入低於 10000,則稅率為 10%。對於收入超過 10000 的部分,超出部分的稅率為 15%。

#include <stdio.h>

int main() {
   int income = 5000;
   float tax;
   printf("Income: %d\n", income);

   if (income<10000){
      tax = (float)(income * 10 / 100);
      printf("tax: %f \n", tax);
   }
   else {
      tax= (float)(1000 + (income-10000) * 15 / 100);
      printf("tax: %f", tax);
   }
}

輸出

執行程式碼並檢查其輸出:

Income: 5000
tax: 500.000000

將 income 變數設定為 15000,然後再次執行程式。

Income: 15000
tax: 1750.000000

示例:使用 if-else 語句檢查數字

以下程式檢查 char 變數是否儲存的是數字字元還是非數字字元。

#include <stdio.h>

int main() {
char ch='7';

   if (ch>=48 && ch<=57){
      printf("The character is a digit.");
   }
   else{
      printf("The character is not a digit.");
   }
   return 0;
}

輸出

執行程式碼並檢查其輸出:

The character is a digit.

將任何其他字元(例如 "*")賦給 "ch" 並檢視結果。

The character is not a digit.

示例:沒有花括號的 if-else 語句

考慮以下程式碼。它旨在在金額大於 100 時計算 10% 的折扣,否則不打折。

#include <stdio.h>

int main() {
   int amount = 50;
   float discount;
   printf("Amount: %d\n", amount);

   if (amount >= 100)
      discount = amount * 10 / 100;
      printf("Discount: %f \n", discount);
   else
      printf("Discount not applicable\n");

   return 0;
}

輸出

程式在編譯期間顯示以下錯誤:

error: 'else' without a previous 'if'

編譯器將執行 if 子句後的第一個語句,並假設由於下一個語句不是 else(無論如何它是可選的),因此後續的 printf() 語句是無條件的。但是,下一個 else 與任何 if 語句都沒有關聯,因此出現錯誤。

示例:沒有花括號的 if-else 語句

也考慮以下程式碼:

#include <stdio.h>

int main() {
   int amount = 50;
   float discount, nett;
   printf("Amount: %d\n", amount);

   if (amount<100)
      printf("Discount not applicable\n");
   else
      printf("Discount applicable");
      discount = amount*10/100;
      nett = amount - discount;
      printf("Discount: %f Net payable: %f", discount, nett);

   return 0;
}

輸出

該程式碼沒有給出任何編譯錯誤,但給出了不正確的輸出:

Amount: 50
Discount not applicable
Discount: 5.000000 Net payable: 45.000000

它產生了不正確的輸出,因為編譯器假設 else 子句中只有一個語句,其餘語句是無條件的。

以上兩個程式碼示例強調了一個事實,即當 ifelse 部分有多個語句時,必須將它們放在花括號中。

為了安全起見,即使對於單個語句,最好也使用花括號。實際上,它提高了程式碼的可讀性。

上面問題的正確解決方案如下所示:

if (amount >= 100){
   discount = amount * 10 / 100;
   printf("Discount: %f \n", discount);
} else {
   printf("Discount not applicable\n");
}

C 語言中的 else-if 語句

C 語言也允許您在程式中使用 else-if。讓我們看看您可能需要在什麼情況下使用 else-if 子句。

假設您遇到如下情況。如果某個條件為真,則執行其後的指定程式碼塊。如果不是,則改為執行下一個程式碼塊。但是,如果以上條件都不滿足,並且所有其他情況都失敗,則最終執行另一個程式碼塊。在這種情況下,您將使用 else-if 子句。

else-if 語句的語法

以下是 else-if 子句的語法:

if (condition){
   // if the condition is true, 
   // then run this code
} else if(another_condition){
   // if the above condition was false 
   // and this condition is true,
   // then run the code in this block

} else{
   // if both the above conditions are false,
   // then run this code
}

else-if 語句的示例

請檢視以下示例:

#include <stdio.h>

int main(void) {
   int age = 15;

   if (age < 18) {
      printf("You need to be over 18 years old to continue\n");
   }else if (age < 21) {
      printf("You need to be over 21\n");
   } else {
      printf("You are over 18 and older than 21 so you can continue \n");
   }
}

輸出

執行程式碼並檢查其輸出:

You need to be over 18 years old to continue

現在,為變數 "age" 提供不同的值,然後再次執行程式碼。如果提供的值小於 18,您將獲得不同的輸出。

c_decision_making.htm
廣告