Important Concept

Loop Control Structures in Salesforce Apex

We will learn about loop control structures in the Salesforce Apex. Loop is a feature that makes the execution of a particular block of code multiple times. Loop control structure in Salesforce Apex is a very important concept used for controlling loops. When you have a known condition to apply on the loops, this loop control structure is very useful to implement those conditions.

Types of Loop Control Structures in Salesforce Apex

There are two types of loop control structures in the Apex, they are

  1. Break
  2. Continue

Now let us discuss the two-loop control structures in the Salesforce Apex.

Break Statement

A break statement inside a loop terminates the execution of statements inside the loop and immediately lets the control come outside the loop. The keyword to use in the break statement is break;.
Let us understand the usage and in detail about the break statement by the below example.

Example

    public static void testing() {
        Integer a = 0;
        for (a = 0; a < 9; a++) {
            if (a == 4) {
                break;
            }
            System.debug(a);
        }
    }

In the above example, we have written a code to print numbers from 0 to 9. So, we have used a for loop and an if loop to understand the exact usage of the break statement. We have created a class named usageOfBreak, inside this class we have declared a variable named ‘a’, and the value of a is assigned to 0. Next, we have used a for loop.

for (a = 0; a < 9; a++) {
    //code
}

A condition was given inside the for loop (i.e., a<9) so that this for loop should execute until the condition becomes false, in the above example the code inside the for loop executes till the value of a is less than 9. Once the value of a becomes 9 the loop terminates.

if (a == 4) {
    break;
}

And if() loop was present inside the for loop. This if loop states that, whenever the value of ‘a’ becomes equal to 4, the code inside the if loop should execute. When the value of ‘a’ becomes 4 then the control goes inside the if loop and executes the statement i.e., break. This statement will break the execution of the loop. Hence the other numbers after 4 will not be printed.

Continue Statement

A continue statement skips the current iteration of the loop and executes with the next value in the loop. The keyword for continue statement is continue;.
Let us learn how to use the continue statement in detail by using the following example.

Example

    public static void testing() {
        Integer a = 0;
        for (a = 0; a < 9; a++) {
            if (a == 4) {
                continue;
            }
            System.debug(a);
        }
    }

The above example is similar to that of the example used to understand the break statement, the only difference is instead of the break statement inside the if loop we have used the continue statement. The for loop executes the same as the previously given example.

if (a == 4) {
    continue;
}

Here, inside the if loop we have written the continue statement, this means that whenever the value of a becomes 4 the if loop executes, and the code inside it also gets executed i.e., the continue statement gets executed. Now if the value of ‘a’ is 4, the continue statement will skip the current iteration and executes with the next values, meaning the value 4 will not be printed. The output will be 0,1,2,3,5,6,7,8.

Conclusion

We have learned about the definition and meaning of loop control structure in Salesforce Apex, types of control structures in it, the usage of break and continue statements with examples and explanation for the examples.

Aniket Malik

Aniket Malik

CERTIFIED TUTOR/TRAINER WITH 300+ REVIEWS

Facing difficulty with

this concept?