Beginner 20 min read

Iterating Lists

By the end of this lesson, you'll be able to:

  • Loop over a List using an enhanced for loop
  • Loop over a List using a classic for loop when the index is needed
  • Build a new List by transforming each item of an existing one

Prerequisites: "Lists"

The default: enhanced for loop

List<String> customerNames = new List<String>{'Amara', 'Ben', 'Carla'};

for (String name : customerNames) {
    System.debug('Processing: ' + name);
}
  1. List<String> customerNames = new List<String>{'Amara', 'Ben', 'Carla'}; — the list to loop over.
  2. (blank line)
  3. for (String name : customerNames) { — this is exactly Module 7's enhanced for loop, now applied to a List directly — no index management needed.
  4. System.debug('Processing: ' + name); — runs once per item.
  5. } — closes the loop.

When the index actually matters

List<String> rankings = new List<String>{'Gold', 'Silver', 'Bronze'};

for (Integer i = 0; i < rankings.size(); i++) {
    System.debug('Position ' + (i + 1) + ': ' + rankings[i]);
}
  1. List<String> rankings = new List<String>{'Gold', 'Silver', 'Bronze'}; — the list.
  2. (blank line)
  3. for (Integer i = 0; i < rankings.size(); i++) { — a classic for loop, needed because the position itself matters here.
  4. System.debug('Position ' + (i + 1) + ': ' + rankings[i]); — i + 1 (since ranks start at 1, not 0) is part of the output — this is exactly the case from Module 7's "Enhanced For Loops" lesson where a classic loop is the better fit, because an enhanced for loop never exposes the current index.
  5. } — closes the loop.

Building a new List from an existing one

List<String> customerNames = new List<String>{'amara', 'ben', 'carla'};
List<String> capitalized = new List<String>();

for (String name : customerNames) {
    capitalized.add(name.capitalize());
}

System.debug(capitalized); // ['Amara', 'Ben', 'Carla']
  1. List<String> customerNames = new List<String>{'amara', 'ben', 'carla'}; — the original list, all lowercase.
  2. List<String> capitalized = new List<String>(); — a brand-new, empty list to collect results into.
  3. (blank line)
  4. for (String name : customerNames) { — loops over the original.
  5. capitalized.add(name.capitalize()); — transforms each item and adds it to the new list — the original customerNames is never modified.
  6. } — closes the loop.
  7. (blank line)
  8. System.debug(capitalized); — logs ['Amara', 'Ben', 'Carla'].

A very common pattern: loop over one List, transforming or filtering each item, and collect the results into a brand-new List.

Exercise

Given List<Integer> prices = new List<Integer>{100, 250, 75}, build a new List<Integer> discountedPrices where each price has 10% subtracted.

Show hint

discountedPrices.add(price - (price * 0.1));

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Iterating Lists Quiz

1. When should you reach for a classic for loop instead of an enhanced for loop over a List?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

Iterating a List means visiting every item in order — the enhanced for loop from Module 7 is the natural default, with the classic for loop reserved for when the index itself matters.