
Loops are a fundamental programming concept that allows you to repeat a block of code multiple times. In Salesforce Apex, loops are used to process large collections of data, automate repetitive tasks, and much more. In this blog post, we’ll cover the different types of loops available in Salesforce Apex, when to use each type, and how to write effective looping structures in Apex.
① For Loop
The for loop is the most commonly used loop in Apex. It’s used to iterate over a collection of items, such as a list or an array. The basic structure of a for loop in Apex is:

Here, data_type
is the type of data stored in the collection, variable_name
is a variable that will hold the current value of the collection being iterated over, and collection
is the collection you want to iterate over.
Here’s an example of a for loop that prints the numbers 1 through 5:

In this example, the for loop iterates over the list numbers
and prints each value to the debug log.
② For Loop with Index
A for loop with an index is similar to a basic for loop, but it keeps track of the index of the current item in the collection. This can be useful when you need to access specific items in a collection or perform operations based on the index of the current item.
Here’s the basic structure of a for loop with an index in Apex:

Here, i is the index of the current item in the collection, 0 is the starting value of i, collection.size() is the number of items in the collection, and the loop continues as long as i is less than the size of the collection.
Here’s an example of a for loop with index that prints the names of people in a list:

In this example, the for loop with index iterates over the list names
and prints each name to the debug log.
③ Enhanced For Loop
The enhanced for loop is a shorthand for the basic for loop. It’s used to iterate over collections and is equivalent to the basic for loop. The basic structure of an enhanced for loop in Apex is:

Here’s an example of an enhanced for loop that prints the numbers 1 through 5:

④ While Loop
The while loop is used to execute a block of code repeatedly as long as a given condition is true. The basic structure of a while loop in Apex is:

Here, condition is the expression that’s evaluated before each iteration of the loop. If the condition is true, the code inside the loop will be executed. If the condition is false, the loop will terminate.
Here’s an example of a while loop that prints the numbers 1 through 5:

In this example, the while loop starts with i equal to 1. The loop continues as long as i is less than or equal to 5. Each iteration of the loop increments i by 1 and prints its value to the debug log.
⑤ Do-While Loop
The do-while loop is similar to a while loop, but it’s guaranteed to execute at least once. The basic structure of a do-while loop in Apex is:

Here, the condition is the expression that’s evaluated after each iteration of the loop. If the condition is true, the code inside the loop will be executed again. If the condition is false, the loop will terminate.
Here’s an example of a do-while loop that prints the numbers 1 through 5:

In this example, the do-while loop starts with I equal to 1. The loop executes the code inside the loop and then evaluates the condition. If the condition is true, the loop will continue. If the condition is false, the loop will terminate.
How to decide which should be used?
The choice of which loop to use in Salesforce Apex depends on the specific requirements of your code. Here are a few guidelines to help you make the best choice:
- For Loops: Use for loops when you know the exact number of iterations required. For example, if you need to loop through a list of 10 records, a for loop would be the most efficient choice.
- While Loops: Use while loops when you don’t know the exact number of iterations required, but you have a condition that must be met. For example, if you need to loop through a list of records until a specific record is found, a while loop would be the most efficient choice.
- Do-While Loops: Use do-while loops when you need to execute a block of code at least once, and then continue the loop based on a condition.
- Time Complexity: When deciding which loop to use, consider the time complexity of each loop. For example, for loops have a time complexity of O(n), where n is the number of iterations. While loops have a time complexity of O(n) in the best-case scenario and O(n^2) in the worst-case scenario, depending on the complexity of the condition.
It’s important to keep in mind that the specific requirements of your code will determine which loop is best. In some cases, it may be necessary to use multiple loops in a single block of code to accomplish a task. The most important thing is to understand the behavior of each loop and choose the one that best meets the needs of your code.
In conclusion, loops are a critical component of any programming language, including Salesforce Apex. Whether you’re processing large collections of data or automating repetitive tasks, loops provide a flexible and efficient solution. By understanding the different types of loops available in Apex, you can write more effective and efficient code that meets the specific needs of your project.
We hope that this guide has provided you with a deeper understanding of the different types of loops in Salesforce Apex, and how to choose the right loop for your code. If you have any further questions or comments, please feel free to reach out in the comments section below. Happy coding!
About the blog
SFDCLessons is a blog where you can find various Salesforce tutorials and tips that we have written to help beginners and experienced developers alike. we also share my experience and knowledge on Salesforce best practices, troubleshooting, and optimization. Don’t forget to follow us on:
Newsletter
Subscribe to our email newsletter to be notified when a new post is published.