
- 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 - while 迴圈
while 迴圈在每次指定條件求值為 true 時執行指令。換句話說,迴圈在程式碼塊執行之前評估條件。作為 JavaScript 的超集,TypeScript 繼承並擴充套件了 JavaScript 的特性,包括不同型別的迴圈。
while 迴圈是一種入口控制迴圈。在入口控制迴圈中,首先檢查條件,如果條件為真,則執行迴圈體內的語句。而在出口控制迴圈中,在執行迴圈體後檢查條件。do...while 迴圈是出口控制迴圈的一個例子。
語法
TypeScript 中 while 迴圈的語法如下:
while(condition) { // statements if the condition is true }
流程圖
while 迴圈的流程圖如下所示:

以下是 while 迴圈行為的分解:
條件評估 - while 迴圈在每次迭代之前評估條件。
執行 - 如果條件評估為 true,則執行迴圈體內的程式碼塊。
迭代 - 程式碼塊執行完成後,迴圈跳回第一步(條件評估)以檢查是否需要另一次迭代。
示例:While 迴圈
var num:number = 5; var factorial:number = 1; while(num >=1) { factorial = factorial * num; num--; } console.log("The factorial is "+factorial);
以上程式碼片段使用 while 迴圈計算變數 num 中值的階乘。
編譯後,它將生成以下 JavaScript 程式碼:
var num = 5; var factorial = 1; while (num >= 1) { factorial = factorial * num; num--; } console.log("The factorial is " + factorial);
它產生以下輸出:
The factorial is 120
帶有 break 語句的 While 迴圈
您可以結合使用 if 語句和 break 語句來提前終止 while 迴圈。if 語句檢查條件。如果條件求值為 true,則 break 語句強制 while 迴圈退出,跳過迴圈體中任何剩餘的程式碼。
示例
在以下示例中,迴圈迭代直到 i 達到 3。當 i 的值變為 3 時,條件 (i === 3) 為真,並且 break 語句終止迴圈。
var i: number = 0; while (i < 5) { if (i === 3) { break; } console.log(i); i++; }
編譯後,它將生成以下 JavaScript 程式碼。
var i = 0; while (i < 5) { if (i === 3) { break; } console.log(i); i++; }
以上示例程式碼的輸出如下:
0 1 2
While 迴圈 vs. for 迴圈
當迭代次數固定且已知時,應使用 for 迴圈。當迭代次數未知時,應使用 while 迴圈。
我們可以透過省略 for 迴圈中的第一個和最後一個表示式來將 for 迴圈轉換為 while 迴圈。
讓我們以以下 for 迴圈為例:
for (var i = 0; i < 5; i++){ console.log(i) }
輸出如下:
0 1 2 3 4
我們可以修改以上示例程式碼如下:
var i = 0 for ( ; i < 5; ){ console.log(i); i++; }
它也將產生與以上程式碼相同的輸出:
0 1 2 3 4
在以上示例中,我們省略了 for 迴圈中的第一個和第三個表示式。它類似於 while 迴圈語句。
var i = 0 while(i < 5){ console.log(i); i++; }
它也將產生與以上兩個示例相同的輸出。
0 1 2 3 4
請注意,沒有第一個和第三個表示式的 for 迴圈類似於 while 迴圈。