目录

while循环

只要给定条件为真,Apex编程语言中的while循环语句就会重复执行目标语句。 这与do-while循环类似,但有一个主要区别。 它只在条件为真时执行代码块,但在do-while循环中,即使条件为假,它也至少执行一次代码块。

语法 (Syntax)

while (Boolean_condition) { execute_code_block }

流程图 (Flow Diagram)

Apex Wile Loop

这里while循环的关键点是循环可能永远不会运行。 当测试条件并且结果为假时,将跳过循环体并且将执行while循环之后的第一个语句。

例子 (Example)

在这个例子中,我们将实现与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++;
}
↑回到顶部↑
WIKI教程 @2018