如何在TypeScript中使用switch語句覆蓋多個if-else條件?


在本教程中,我們將學習如何使用TypeScript中的switch case語句來覆蓋多個if-else條件。單個if-else語句用於執行條件語句。如果條件為真,則執行if塊的語句;否則,控制權將回退到else塊並執行其語句。

在某些情況下,開發者需要根據不同的條件執行程式碼塊。為此,他們需要編寫多個if-else語句或if-else語句的階梯結構。在這裡,我們將把這些if-else語句階梯結構轉換為switch case語句。

將If-else條件轉換為TypeScript中的Switch語句

這裡,我們取了一個年齡陣列,並根據人的年齡顯示不同的詞語,使用了if-else階梯結構。

// Array of ages
let ages: Array<number> = [32, 5, 12, 34, 54, 65, 76];
// if-else ladder for different range of edges
for (let age of ages) {
   if (age <= 10) {
      // print childern
   } else if (age > 10 && age <= 20) {
      // print younger
   } else if (age > 20 && age <= 60) {
      // print mature man
   } else {
      // print old man
   }
}

現在,我們將上面的if-else階梯結構轉換為switch case語句。switch case語句將值作為引數,我們為此建立了不同的case。這裡,我們使用不同的case的條件,這些條件返回布林值。因此,我們將'true'布林值作為switch case的引數。

// Array of ages
let ages: Array<number> = [32, 5, 12, 34, 54, 65, 76];
// Switch case statement for different range of edges
for (let age of ages) {
   switch (true) {
      case age <= 10:
      // print children
      break;
      case age > 10 && age <= 20:
      // print younger
      break;
      case age > 20 && age <= 60:
      // print mature man
      break;
      default:
      // print old man
   }
}

在上面的語法中,我們對不同年齡的case使用了switch case語句。

示例1

在下面的示例中,我們建立了一個包含從0到80的不同數字的年齡陣列。之後,我們使用for迴圈迭代陣列元素,並對每個元素評估switch case語句。

此外,使用者可以看到我們使用了break關鍵字來中斷語句,以及default case,它等於if-else階梯結構中的最後一個else塊。在輸出中,使用者可以看到它根據年齡的值列印不同的詞語。

let ages: Array<number> = [32, 5, 12, 34, 54, 65, 76];
for (let age of ages) {
   switch (true) {
      case age <= 10:
         console.log(age + " = child");
         break;
      case age > 10 && age <= 20:
         console.log(age + " = younger");
         break;
      case age > 20 && age <= 60:
         console.log(age + " = mature man");
         break;
      default:
      console.log(age + " =  old man");
   }
}

編譯後,它將生成以下JavaScript程式碼:

var ages = [32, 5, 12, 34, 54, 65, 76];
for (var _i = 0, ages_1 = ages; _i < ages_1.length; _i++) {
   var age = ages_1[_i];
   switch (true) {
      case age <= 10:
         console.log(age + " = child");
         break;
      case age > 10 && age <= 20:
         console.log(age + " = younger");
         break;
      case age > 20 && age <= 60:
         console.log(age + " = mature man");
         break;
      default:
         console.log(age + " =  old man");
   }
}

輸出

以上程式碼將產生以下輸出:

32 = mature man
5 = child
12 = younger
34 = mature man
54 = mature man
65 =  old man
76 =  old man

示例2

在下面的示例中,我們建立了一個名為Websites的列舉,其中包含不同的網站。之後,我們建立了一個名為web的變數,它包含來自列舉的'TutorialsPoint'網站的值。

getWebsites()函式使用switch case語句根據web變數的值列印值。基本上,使用者可以從下面的示例中學習如何在switch case語句中使用列舉值。

// Creating the enum for different sites
enum Websites {
   TutorialsPoint,
   Tutorix,
}
// Creating the web variable containing the value TutorialsPoint
var web: Websites = Websites.TutorialsPoint;

// function containing the switch case statement
function getWebsites() {
   // Switch case statement for web variable
   switch (web) {
      case Websites.TutorialsPoint:
         console.log("You are on TutorialsPoint Website.");
         break;
      case Websites.Tutorix:
         console.log("You are on Tutorix Website.");
         break;
   }
}

getWebsites(); // get websites

編譯後,它將生成以下JavaScript程式碼:

// Creating the enum for different sites
var Websites;
(function (Websites) {
   Websites[Websites["TutorialsPoint"] = 0] = "TutorialsPoint";
   Websites[Websites["Tutorix"] = 1] = "Tutorix";
})(Websites || (Websites = {}));
// Creating the web variable containing the value TutorialsPoint
var web = Websites.TutorialsPoint;
// function containing the switch case statement
function getWebsites() {
   // Switch case statement for web variable
   switch (web) {
      case Websites.TutorialsPoint:
         console.log("You are on TutorialsPoint Website.");
            break;
      case Websites.Tutorix:
         console.log("You are on Tutorix Website.");
            break;
   }
}
getWebsites(); // get websites

輸出

以上程式碼將產生以下輸出:

You are on TutorialsPoint Website.

在這個TypeScript教程中,使用者學習瞭如何將if-else階梯結構轉換為switch case語句。在第一個示例中,使用者學習瞭如何使用條件語句作為switch語句的case。在第二個示例中,使用者學習瞭如何使用列舉值作為switch語句的case。

此外,使用者還學習瞭如何在TypeScript中使用break和default關鍵字與switch case。

更新於:2022年12月19日

2K+瀏覽量

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告