What happens after you have completed the SET test

Using loops

learning goals

After completing this lesson, you will be able to do the following tasks:

  • Differentiate between "while" and "do-while" loops
  • Use increment and decrement operators to control the execution of code

In our Teatime example, we made a pot of tea to share with our closest friends. But what if we have to serve tea to a group of 100 people? We have to repeat the same steps over and over until everyone is taken care of. This is going to make a lot of redundant code, right? This is where the practical loops come into play.

A loop is a block of code that is repeated until a certain condition is met. In the case of the teatime code, let's go through the steps that need to be repeated:

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

Repeating steps can be added to a loop. Apex recognizes three types of loop code:,, and. For now, let's focus on - and - loops. With and loops, it's not just the names that are very similar. Both check whether a certain condition is met. The distinguishing feature is when they check whether the condition is met. -Loops check the condition, before the loop starts, -loops check them, after this the loop has ended.

Looks like a little detail, doesn't it? But that actually has a pretty big impact.

Think of it this way: The loop is always run through at least once. The loop may never go through, depending on the condition.

while loops

The loop begins by checking whether a condition is met. If the condition is true, something happens. If it is wrong, the loop exits.

Here is the syntax:

While (condition) {// run this block of code}

Let's apply loops to our teatime code.

  1. In the Developer Console, click Debug | Open Execute Anonymous Window.
  2. Copy and paste this code into the Enter Apex Code window.
    // 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'); // add sugar totalAmountSugar--; // add tea totalAmountTea--; // check ingredients if (totalAmountSugar == 0 || totalAmountTea == 0) {System.debug ('Out of ingredients!'); break; // ends the While loop} totalGuests--; }
  3. Check the box Open log and then click Execute.

    The execution log is opened with the result of the execution of the code.

  4. Check the box at the bottom of the window Debug Only.

No, the sign is not a typo. It is known as the post-decrement operator. It's a shorthand way of saying, "Subtract a number from this value." If it equals 159, decrease the value so that the value from now on is 158. The post-increment operator,, does the opposite, adding a number to the value.

So what exactly is going on here? Remember, we have one main goal, which is to serve tea to 100 guests. Every time we serve a guest, a portion of each ingredient (and) and a guest () are deducted. If or reaches 0 (line 21) OR 0 (line 12), the loop is ended and no one else is served.

Let's see how the loop works.

do-while loops

A loop allows the code to perform a task once before testing the condition.

The loop begins with the one-time execution of a task. Next, a condition is checked. If the condition is true, the task will run again. If it is wrong, the loop exits.

Let's look at the syntax:

Do {// run this block of code} while (condition);
  1. In the Developer Console, click Debug | Open Execute Anonymous Window.
  2. Copy and paste this code into the Enter Apex Code window.
    // 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; do {// 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);
  3. Check the box Open log and then click Execute.

    The execution log is opened with the result of the execution of the code.

  4. Check the box at the bottom of the window Debug Only.

You've declared variables, instantiated values, created lists, and looped through data in various ways. Congratulations! You have completed the Introduction to Apex and are well on your way to understanding code better.

resources