for Loop – Operators and Control Statements – Apex Programming – Salesforce Certified Platform Developer I Study Guide

for Loop: The for loop is used to iterate over a range of values for a specified number of times. It is a procedural loop used to execute a block of code repeatedly for a predetermined number of iterations.

for (Integer i = 0; i < 10; i++) { System.debug(‘The value of i is: ‘ + i);

}

This loop will iterate 10 times, with the value of i starting at 0 and incrementing by 1 each time through the loop. The System.debug() statement will print the value of i to the debug log each time through the loop.

In this example, i is an integer variable that is used to keep track of the current iteration of the loop. The loop will continue to execute as long as i is less than 10. The System.debug() statement is just an example of code that could be executed inside the loop. Any valid Apex code could be used inside the loop to process data or perform other tasks.

Here’s an another example using the for loop in Apex to iterate through a list of numbers:

List<Integer> numbers = new List<Integer>{1, 2, 3, 4, 5};

for (Integer num : numbers) {

System.debug(‘Number: ‘ + num);

}

In this example:

–– numbers is a list containing integers from 1 to 5.

–– The for loop iterates over each element in the numbers list.

–– The loop variable num is initialized with the value of each element in the list during each iteration.

–– The code inside the loop body prints the value of num.

The output of this code would be

Number: 1

Number: 2

Number: 3

Number: 4

Number: 5

The for loop is particularly useful when you know the number of iterations you need to perform or when you’re iterating over a specific sequence of values. If you don’t know the exact number of iterations or you need to dynamically determine when to stop, you might want to use a while loop instead.

while Loop: The while loop as a procedural loop repeatedly executes a block of code as long as a specified condition is true. Unlike the for loop, which is often used when you know the number of iterations in advance, the while loop is useful when you want to continue looping until a specific condition is no longer met. Here’s how the while loop works in Apex:

Integer count = 2;

while (count <= 6) {

System.debug(‘Number: ‘ + count);

count += 2;

}

In this example:

–– The loop starts with the count variable initialized to 2.

–– The while loop continues to execute as long as the condition count <= 6 is true.

–– In each iteration, the loop prints the current value of count and then increments it by 2.

–– Once count becomes 6, the condition count <= 6 becomes false, and the loop terminates.

The output of this code would be

Number: 2

Number: 4

Number: 6

It’s important to ensure that the condition in a while loop eventually becomes false, or else you might end up in an infinite loop that never stops executing. To prevent this, make sure that the condition is being manipulated within the loop, such as by incrementing or decrementing a loop variable, so that it eventually changes and the loop terminates.

The choice between using a for loop and a while loop depends on the situation. If you know the number of iterations beforehand or have a specific sequence to iterate over, a for loop might be more appropriate. If you need to keep looping until a particular condition changes, a while loop is a better choice.