Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I just have a simple question about how list for loops work that has been bugging me. Does the for loop variable refer to the actual list item? Here's what I mean:

Say I have a list of accounts.

List<Account> accountList = new List<Account>();
accountList = [SELECT Id FROM Account];

And I write a for loop like so:

for(Account a: accountList){
  a.Name = 'whatever';
update accountList;

Is that the same as:

for(Integer a = 0; a < accountList.size(); a++){
  accountList[a].Name = 'whatever';
update accountList;

In other words, is a in the first loop just a shorthand for accountList[a] ?

Thanks for answering this admittedly basic question. The Apex workbook wasn't very explicit, and I had a hard time Googling it.

share|improve this question
up vote 37 down vote accepted

No. They are not the same thing. There are differences in the way they work.

In your example, they accomplish the same thing - you do indeed get a reference in each case to the element you want, and you are able to manipulate it. Maybe that's what you were asking, but it's important to understand the differences between the constructs.

In the enhanced for loop, under the covers, you are given an iterator which is backed by your list. This iterator tracks where you are in the underlying list and gives you the current / next item. In Apex, as in Java, iterators are convenient but they have a drawback: you can't modify the underlying collection during iteration. (More on this in a sec.)

In a "normal" index-based for loop, all you are doing is incrementing a counter each time, and then (if you wish), using that counter as the index into a list.

In general, the enhanced for loop is easier for developers and saves you a few characters of code and looks prettier. However, there are two very common use cases where you can't use an enhanced for loop.

1) When you want to add/remove items from the collection during iteration. This trivial example will throw a "Cannot modify a collection while it is being iterated." exception in Apex:

List<String> testloop = new List<String>();
for (String astring : testloop) {

But this will not:

List<String> testloop = new List<String>();
for (Integer i = 0; i < testloop.size(); i++) {

(The second example, besides being horribly inefficient, also demonstrates why you need to be careful when modifying lists during iteration - it has a bug in it.)

2) When you need to get at matching elements in two separate lists at the same time. This is VERY common in Apex.

for (Integer i = 0; i < testloop.size(); i++) { // testloop and testloop2 are known to be identical in size
    if (testloop[i] != testloop2[i]) {
        // do something

Hope that helps.

share|improve this answer
NICE examples 1) and 2) - candidate for Populist badge! – bigassforce Feb 8 '13 at 1:54
Example 2 begs a discussion of using a list and a map to sync up access to the same sObject. But that would have been a bit off topic. Great answer! – Peter Feb 8 '13 at 10:55

Yes this is a feature in JAVA and C# which is also available in APEX.

Search for "for-each" loop.

Basically the first type of for loop is very convenient in APEX development when you need to iterate over all objects in the list and perform some operation like in your example.

I rarely use the second type of for loop in APEX but it is essentially the same IMO.

Also see:

share|improve this answer
Thank you, I went straight from (beginner) C++ to APEX, with no SQL experience either, and it seems most of the documentation assumes JAVA/C# and MySQL knowledge, so sometimes these basic things aren't well explained. – Aaron P. Feb 7 '13 at 23:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.