Apex - do-while 循环
与在循环顶部测试循环条件的 for 和 while 循环不同,do...while 循环在循环底部检查其条件。< /p>
do...while 循环与 while 循环类似,不同之处在于 do...while 循环保证至少执行一次。
语法
do { code_to_execute } while (Boolean_condition);
流程图
示例
我们将更新列表中唯一的前 1 条记录,仅此而已。
// Code for do while loop List<apex_invoice__c> InvoiceList = [SELECT Id, APEX_Description__c, APEX_Status__c FROM APEX_Invoice__c LIMIT 20]; //it will fetch only 20 records Integer i = 0; do { InvoiceList[i].APEX_Description__c = 'This is the '+i+' Invoice'; // This will print the updated description in debug log System.debug('****Updated Description'+InvoiceList[i].APEX_Description__c); i++; // Increment the counter } while (i< 1); // iterate till 1st record only