当前位置: 首页 > 文档资料 > Apex 中文教程 >

Java-like for循环

优质
小牛编辑
122浏览
2023-12-01

Apex中有一个传统的类似Java的for循环。

语法 (Syntax)

for (init_stmt; exit_condition; increment_stmt) { code_block }

流程图 (Flow Diagram)

Apex For Loop

例子 (Example)

请考虑以下示例以了解传统for循环的用法 -

// The same previous example using For Loop
// initializing the custom object records list to store the Invoice Records
List<apex_invoice__c> PaidInvoiceNumberList = new List<apex_invoice__c>();
PaidInvoiceNumberList = [SELECT Id,Name, APEX_Status__c FROM APEX_Invoice__c WHERE 
   CreatedDate = today];
// this is SOQL query which will fetch the invoice records which has been created today
List<string> InvoiceNumberList = new List<string>();
// List to store the Invoice Number of Paid invoices
for (Integer i = 0; i < paidinvoicenumberlist.size(); i++) {
   // this loop will iterate on the List PaidInvoiceNumberList and will process
   // each record. It will get the List Size and will iterate the loop for number of
   // times that size. For example, list size is 10.
   if (PaidInvoiceNumberList[i].APEX_Status__c == 'Paid') {
      // Condition to check the current record in context values
      System.debug('Value of Current Record on which Loop is iterating is 
         '+PaidInvoiceNumberList[i]);
      //current record on which loop is iterating
      InvoiceNumberList.add(PaidInvoiceNumberList[i].Name);
      // if Status value is paid then it will the invoice number into List of String
   }
}
System.debug('Value of InvoiceNumberList '+InvoiceNumberList);

执行步骤 (Execution Steps)

执行此类型的for loop ,Apex运行时引擎执行以下步骤 -

  • 执行循环的init_stmt组件。 请注意,可以在此语句中声明和/或初始化多个变量。

  • 执行exit_condition检查。 如果为true,则循环继续;如果为false,则循环退出。

  • 执行code_block 。 我们的代码块是打印数字。

  • 执行increment_stmt语句。 它会每次增加。

  • 返回第2步。

作为另一个示例,以下代码将数字1 - 100输出到调试日志中。 请注意,包含一个额外的初始化变量j来演示语法:

//this will print the numbers from 1 to 100}
for (Integer i = 0, j = 0; i < 100; i++) { System.debug(i+1) };

考虑因素(Considerations)

执行此类型的for loop语句时,请考虑以下几点。

  • 迭代时我们无法修改集合。 假设您正在迭代列表'ListOfInvoices' ,那么在迭代时您无法修改同一列表中的元素。

  • 您可以在迭代时在原始列表中添加元素,但是您必须在迭代时将元素保留在临时列表中,然后将这些元素添加到原始列表中。

最后更新:

类似资料

  • 您好,我对jquery没有什么问题。首先,我有: 大众BORA 1.9TDI 1990 1995 奥迪A3 2.0TFSI 2006 2008 但我想实现: VW BORA 1.9TDI 1990 VW BORA 1.9TDI 1991 VW BORA 1.9TDI 1992 VW BORA 1.9TDI 1993 VW BORA 1.9TDI 1994 VW BORA 1.9TDI 1995 A

  • 问题内容: 我第一次不了解PHP。我一直在脚本中使用for循环,while循环,foreach循环。我想知道 哪一个性能更好? 选择循环的标准是什么? 当我们在另一个循环中循环时应该使用哪个? 我一直想知道要使用哪个循环的代码。 很明显,我可以使用while编写上面的代码。希望有人能帮助我找出哪个循环更适合使用。 问题答案: 哪一个性能更好? 没关系 选择循环的标准是什么? 如果只需要遍历对象或数

  • 通常你想在一个任务中干很多事,比如创建一群用户,安装很多包,或者重复一个轮询步骤直到收到某个特定结果. 本章将对在playbook中如何使用循环做全面的介绍. Topics 循环 为了保持简洁,重复的任务可以用以下简写的方式: - name: add several users user: name={{ item }} state=present groups=wheel with_it

  • 循环其实不足为奇。跟其它程序设计语言一样,bash中的循环也是只要控制条件为真就一直迭代执行的代码块。 Bash中有四种循环:for,while,until和select。 for循环 for与它在C语言中的姊妹非常像。看起来是这样: for arg in elem1 elem2 ... elemN do # 语句 done 在每次循环的过程中,arg依次被赋值为从elem1到elemN。这些

  • 尽管已经支持JavaScript原生代码,Jade还是支持了一些特殊的标签,它们可以让模板更加易于理解,其中之一就是each, 这种形式: each VAL[, KEY] in OBJ 一个遍历数组的例子 : - var items = ["one", "two", "three"] each item in items li= item 渲染为: <li>one</li> <li>two</

  • 可能存在需要多次执行代码块的情况。 通常,语句按顺序执行:首先执行函数中的第一个语句,然后执行第二个语句,依此类推。 编程语言提供各种控制结构,允许更复杂的执行路径。 循环语句允许我们多次执行语句或语句组,以下是大多数编程语言中循环语句的一般形式 - Perl编程语言提供以下类型的循环来处理循环要求。 Sr.No. 循环类型和描述 1 while 循环 在给定条件为真时重复语句或语句组。 它在执行