• 使用递增和递减运算符来控制代码执行。
  • 在我们给出的 Teatime 示例中,我们泡了一壶茶来招待我们最亲密的朋友。但是,如果我们需要为 100 人提供茶,会怎样呢?我们需要一遍又一遍地重复相同的步骤,直到每个人都得到茶水。听起来像是要写很多重复的代码,对不对?这时循环功能就可以派上用场了!

    循环是一个代码块,会在某个条件得到满足之前一直重复执行。以 Teatime 代码为例,让我们回顾一下需要重复的步骤:

    Add Tea and Sugar to Teacup
    Pour Tea in Teacup
    Put 1 teaspoon of Sugar in Teacup
    Stir Tea in Teacup
    Serve Tea to a Friend

    重复的步骤可以添加到循环中。Apex 提供三种循环代码的方法: while do-while for 循环。当前,我们主要讨论 while do-while 循环。就像它们的名字一样, while do-while 循环非常相似。两者都是在判断是否满足特定条件。不同之处在于它们判断条件是否得到满足的 时间点 While 循环在循环开始 之前 检查条件,而 do-while 循环在循环结束 之后 检查条件。

    这似乎只是一个小细节,对吧?事实上,带来的影响相当大。

    您可以这样理解: do-while 循环至少会执行一次。而 while 循环取决于具体条件可能永远不会执行。

    While 循环

    While 循环首先判断条件是否得到满足。如果条件为真,则执行一些操作。如果条件为假,循环停止。

    让我们将 while 循环应用于我们的 Teatime 代码。

  • 在 Developer Console 中,单击 Debug(调试) | Open Execute Anonymous Window(打开执行匿名窗口)
  • 复制此代码并将其粘贴到“输入 Apex 代码”窗口中。
    //Declare an Integer variable called totalGuests and set it to 100
    Integer totalGuests = 100;
    //Declare an Integer variable called totalAmountSugar and set it to 159 (number of teaspoons in a bag of sugar).
    Integer totalAmountSugar = 159;
    //Declare an Integer variable called totalAmountTea and set it to 35 (number of teabags).
    Integer totalAmountTea = 35;
    //Loop: Add a teaspoon of sugar and one tea bag to a tea cup. Serve until there is no sugar or tea left.
    While(totalGuests > 0) {
        System.debug(totalGuests + ' Guests Remaining');
        //check ingredients
        if(totalAmountSugar == 0 || totalAmountTea == 0) {
            System.debug('Out of ingredients! Sugar: ' + totalAmountSugar + ' Tea: ' + totalAmountTea);
            break; //ends the While loop
        //add sugar
        totalAmountSugar--;
        //add tea
        totalAmountTea--;
        //guest served
        totalGuests--;
      
  • 选择打开日志复选框,然后单击执行。执行日志将打开,显示代码运行的结果。
  • 选择窗口底部的仅调试复选框。
  • 不,-- 符号并非打错了。它是后递减运算符。这是“从这个值中减去一”的简要说法。如果 totalAmountSugar 等于 159,则将其递减,使 totalAmountSugar 当前的值等于 158。后递增运算符 ++ 的作用则相反,它会将一个值加一。

    接下来会发生什么呢?记住我们的主要目标:为 100 位客人提供茶水。每当我们为一位客人提供茶水后,需提供的每种原料 (totalAmountSugar and totalAmountTea) 和服务的客人 (totalGuests) 的数量将减一。当 totalAmountSugartotalAmountTea 变为 0(第 15 行)或 totalGuests 变为 0(第 12 行)后,循环停止,不再为任何人提供服务。 

    让我们看看 do-while 循环的工作方式。

    Do While 循环

    Do-while 循环允许代码在测试条件之前执行一次操作。

    Do-while 循环从执行一次任务开始。接下来,判断一个条件。如果条件为真,则再次执行任务。如果条件为假,循环停止。

    看看涉及的语法:

    //run this block of code } while(condition);
  • 在 Developer Console 中,单击 Debug(调试) | Open Execute Anonymous Window(打开执行匿名窗口)
  • 复制此代码并将其粘贴到“输入 Apex 代码”窗口中。
    //Declare an Integer variable called totalGuests and set it to 100
    Integer totalGuests = 100;
    //Declare an Integer variable called totalAmountSugar and set it to 159 (number of teaspoons in a bag of sugar).
    Integer totalAmountSugar = 159;
    //Declare an Integer variable called totalAmountTea and set it to 35 (number of teabags).
    Integer totalAmountTea = 35;
        //deduct sugar serving
        totalAmountSugar--;
        //deduct tea serving
        totalAmountTea--;
        //deduct guest
        totalGuests--;
        System.debug(totalGuests + ' Guests Remaining!');
        //check ingredients
        if(totalAmountSugar == 0 || totalAmountTea == 0) {
            System.debug('Out of ingredients!');
            break; //ends the While loop
    } while(totalGuests > 0);
  • 选择 打开日志 复选框,然后单击 执行 。执行日志将打开,显示代码运行的结果。
  • 选择窗口底部的 仅调试 复选框。
  • 您已经学会通过多种方式声明变量、实例化值、创建列表和循环数据。恭喜!您已经对 Apex 有初步了解,并且正在深入了解代码。

  • Apex 开发人员指南 :循环
  •