Beginner 20 min read

Enhanced For Loops

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

  • Write an enhanced for loop over a List
  • Explain when an enhanced for loop is preferable to a classic for loop
  • Recognize that an enhanced for loop cannot access the current index

Prerequisites: "For Loops"

The shape

List<String> customerNames = new List<String>{'Acme Corp', 'Globex', 'Initech'};

for (String customerName : customerNames) {
    System.debug('Welcome, ' + customerName);
}
  1. List<String> customerNames = new List<String>{...}; — three names.
  2. (blank line)
  3. for (String customerName : customerNames) { — read as "for each String in customerNames, call it customerName." There's no counter to initialize, check, or increment — Apex handles walking through the list for you.
  4. System.debug('Welcome, ' + customerName); — runs once per name, with customerName holding a different value each pass.
  5. } — closes the loop.

A real business example: Nonprofit (Donor Management)

List<Decimal> donationAmounts = new List<Decimal>{50, 250, 1000, 25};
Decimal totalRaised = 0;

for (Decimal amount : donationAmounts) {
    totalRaised += amount;
}

System.debug('Total raised: ' + totalRaised);
  1. List<Decimal> donationAmounts = new List<Decimal>{50, 250, 1000, 25}; — four donations.
  2. Decimal totalRaised = 0; — the running total, starting at zero.
  3. (blank line)
  4. for (Decimal amount : donationAmounts) { — one pass per donation amount.
  5. totalRaised += amount; — adds this pass's amount to the running total.
  6. } — closes the loop.
  7. (blank line)
  8. System.debug('Total raised: ' + totalRaised); — logs the final total, 1325, after all four amounts have been added.

Summing a batch of donations doesn't need index tracking at all — just "look at each amount, add it to the running total" — which is exactly what an enhanced for loop expresses.

When you still need a classic for loop

An enhanced for loop never tells you which position you're at — there's no i to reference. If you need the index (say, to only process every other item, or to compare an item to the one before it), reach for the classic for loop from the previous lesson instead.

Exercise

Given List<Integer> quantities = new List<Integer>{4, 8, 15, 16, 23}, use an enhanced for loop to debug each quantity.

Show hint

for (Integer quantity : quantities) { ... }

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Enhanced For Loops Quiz

1. What can an enhanced for loop NOT give you directly?

Log in to submit the quiz and save your score.

Leaderboard

  • 1. Monde Masiza 1/1

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

The enhanced for loop ("for each") iterates directly over a collection's items without managing a counter at all — the most common loop shape you'll actually write against Salesforce data.