
- TypeScript 基礎
- TypeScript - 首頁
- TypeScript - 路線圖
- TypeScript - 概述
- TypeScript - 環境設定
- TypeScript - 基本語法
- TypeScript vs. JavaScript
- TypeScript - 特性
- TypeScript - 變數
- TypeScript - let & const
- TypeScript - 運算子
- TypeScript 基本型別
- TypeScript - 型別
- TypeScript - 型別註解
- TypeScript - 型別推斷
- TypeScript - 數字
- TypeScript - 字串
- TypeScript - 布林值
- TypeScript - 陣列
- TypeScript - 元組
- TypeScript - 列舉
- TypeScript - Any
- TypeScript - Never
- TypeScript - 聯合型別
- TypeScript - 字面量型別
- TypeScript - 符號
- TypeScript - null vs. undefined
- TypeScript - 類型別名
- TypeScript 控制流
- TypeScript - 決策
- TypeScript - If 語句
- TypeScript - If Else 語句
- TypeScript - 巢狀 If 語句
- TypeScript - Switch 語句
- TypeScript - 迴圈
- TypeScript - For 迴圈
- TypeScript - While 迴圈
- TypeScript - Do While 迴圈
- TypeScript 函式
- TypeScript - 函式
- TypeScript - 函式型別
- TypeScript - 可選引數
- TypeScript - 預設引數
- TypeScript - 匿名函式
- TypeScript - 函式構造器
- TypeScript - Rest 引數
- TypeScript - 引數解構
- TypeScript - 箭頭函式
- TypeScript 介面
- TypeScript - 介面
- TypeScript - 擴充套件介面
- TypeScript 類和物件
- TypeScript - 類
- TypeScript - 物件
- TypeScript - 訪問修飾符
- TypeScript - 只讀屬性
- TypeScript - 繼承
- TypeScript - 靜態方法和屬性
- TypeScript - 抽象類
- TypeScript - 訪問器
- TypeScript - 鴨子型別
- TypeScript 高階型別
- TypeScript - 交叉型別
- TypeScript - 型別守衛
- TypeScript - 型別斷言
- TypeScript 型別操作
- TypeScript - 從型別建立型別
- TypeScript - Keyof 型別運算子
- TypeScript - Typeof 型別運算子
- TypeScript - 索引訪問型別
- TypeScript - 條件型別
- TypeScript - 對映型別
- TypeScript - 模板字面量型別
- TypeScript 泛型
- TypeScript - 泛型
- TypeScript - 泛型約束
- TypeScript - 泛型介面
- TypeScript - 泛型類
- TypeScript 其他
- TypeScript - 三斜線指令
- TypeScript - 名稱空間
- TypeScript - 模組
- TypeScript - 環境宣告
- TypeScript - 裝飾器
- TypeScript - 型別相容性
- TypeScript - Date 物件
- TypeScript - 迭代器和生成器
- TypeScript - Mixins
- TypeScript - 實用程式型別
- TypeScript - 裝箱和拆箱
- TypeScript - tsconfig.json
- 從 JavaScript 到 TypeScript
- TypeScript 有用資源
- TypeScript - 快速指南
- TypeScript - 有用資源
- TypeScript - 討論
TypeScript - 迴圈
您可能會遇到需要多次執行程式碼塊的情況。通常,語句是按順序執行的:函式中的第一個語句首先執行,然後是第二個語句,依此類推。
程式語言提供了各種控制結構,允許更復雜的執行路徑。
迴圈語句允許我們多次執行語句或語句組。下面是在大多數程式語言中迴圈語句的一般形式。

TypeScript 提供不同型別的迴圈來處理迴圈需求。下圖說明了迴圈的分類 -

確定迴圈
迭代次數確定/固定的迴圈稱為確定迴圈。for 迴圈是確定迴圈的一種實現。
序號 | 迴圈和描述 |
---|---|
1. | for 迴圈
for 迴圈是確定迴圈的一種實現。 |
不確定迴圈
當迴圈中的迭代次數不確定或未知時,使用不確定迴圈。
不確定迴圈可以使用 -
序號 | 迴圈和描述 |
---|---|
1. | while 迴圈
while 迴圈在每次指定的條件計算結果為真時執行指令。 |
2. | do… while
do…while 迴圈與 while 迴圈類似,只是 do...while 迴圈在第一次執行迴圈時不會計算條件。 |
示例:while 與 do..while
var n:number = 5 while(n > 5) { console.log("Entered while") } do { console.log("Entered do…while") } while(n>5)
此示例最初聲明瞭一個 while 迴圈。只有當傳遞給 while 的表示式計算結果為真時,才會進入迴圈。在此示例中,n 的值不大於零,因此表示式返回 false,並且跳過迴圈。
另一方面,do…while 迴圈會執行一次語句。這是因為初始迭代不考慮布林表示式。但是,對於後續迭代,while 會檢查條件並將控制權移出迴圈。
編譯後,它將生成以下 JavaScript 程式碼 -
var n = 5; while (n > 5) { console.log("Entered while"); } do { console.log("Entered do…while"); } while (n > 5);
以上程式碼將產生以下輸出 -
Entered do…while
break 語句
break 語句用於將控制權移出構造。在迴圈中使用break 會導致程式退出迴圈。其語法如下 -
語法
break
流程圖

示例
現在,看看下面的示例程式碼 -
var i:number = 1 while(i<=10) { if (i % 5 == 0) { console.log ("The first multiple of 5 between 1 and 10 is : "+i) break //exit the loop if the first multiple is found } i++ } //outputs 5 and exits the loop
編譯後,它將生成以下 JavaScript 程式碼 -
var i = 1; while (i <= 10) { if (i % 5 == 0) { console.log("The first multiple of 5 between 1 and 10 is : " + i); break; //exit the loop if the first multiple is found } i++; } //outputs 5 and exits the loop
它將產生以下輸出 -
The first multiple of 5 between 1 and 10 is : 5
continue 語句
continue 語句跳過當前迭代中的後續語句,並將控制權返回到迴圈的開頭。與 break 語句不同,continue 不會退出迴圈。它終止當前迭代並開始後續迭代。
語法
continue
流程圖

示例
continue 語句的示例如下 -
var num:number = 0 var count:number = 0; for(num=0;num<=20;num++) { if (num % 2==0) { continue } count++ } console.log (" The count of odd values between 0 and 20 is: "+count) //outputs 10
以上示例顯示了 0 到 20 之間奇數值的數量。如果數字為偶數,則迴圈退出當前迭代。這是使用continue 語句實現的。
編譯後,它將生成以下 JavaScript 程式碼。
var num = 0; var count = 0; for (num = 0; num <= 20; num++) { if (num % 2 == 0) { continue; } count++; } console.log(" The count of odd values between 0 and 20 is: " + count); //outputs 10
輸出
The count of odd values between 0 and 20 is: 10
無限迴圈
無限迴圈是一個無限執行的迴圈。for 迴圈和while 迴圈可用於建立無限迴圈。
語法:使用 for 迴圈建立無限迴圈
for(;;) { //statements }
示例:使用 for 迴圈建立無限迴圈
for(;;) { console.log(“This is an endless loop”) }
語法:使用 while 迴圈建立無限迴圈
while(true) { //statements }
示例:使用 while 迴圈建立無限迴圈
while(true) { console.log(“This is an endless loop”) }