
- Apex 程式設計教程
- Apex - 主頁
- Apex - 概述
- Apex - 環境
- Apex - 示例
- Apex - 資料型別
- Apex - 變數
- Apex - 字串
- Apex - 陣列
- Apex - 常量
- Apex - 決策制定
- Apex - 迴圈
- Apex - 集合
- Apex - 類
- Apex - 方法
- Apex - 物件
- Apex - 介面
- Apex - DML
- Apex - 資料庫方法
- Apex - SOSL
- Apex - SOQL
- Apex - 安全性
- Apex - 呼叫
- Apex - 觸發器
- Apex - 觸發器設計模式
- Apex - 監管限制
- Apex - 批處理
- Apex - 除錯
- Apex - 測試
- Apex - 部署
- Apex 實用資源
- Apex - 快速指南
- Apex - 資源
- Apex - 討論
Apex - While 迴圈
Apex 程式語言中的一個 while 迴圈語句會重複執行目標語句,只要給定條件為真。這在某種程度上類似於 do-while 迴圈,但有一個主要區別。它只有在條件為真時才會執行程式碼塊,但在 do-while 迴圈中,即使條件為假,它也會至少執行一次程式碼塊。
語法
while (Boolean_condition) { execute_code_block }
流程圖

此處 while 迴圈的關鍵點在於迴圈可能永遠不會執行。當條件得到測試且結果為假時,迴圈體將被跳過並且 while 迴圈後的第一條語句將被執行。
示例
在該示例中,我們將實現與針對 do-while 迴圈所做的相同的場景,但這次將使用 While 迴圈。它將更新 10 條記錄的描述。
//Fetch 20 records from database List<apex_invoice_c> InvoiceList = [SELECT Id, APEX_Description_c, APEX_Status_c FROM APEX_Invoice_c LIMIT 20]; Integer i = 1; //Update ONLY 10 records while (i< 10) { InvoiceList[i].APEX_Description__c = 'This is the '+i+'Invoice'; System.debug('Updated Description'+InvoiceList[i].APEX_Description_c); i++; }
apex_loops.htm
廣告