Beginner 20 min read

For Loops

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

  • Write a classic for loop with initializer, condition, and increment
  • Trace through a for loop step by step
  • Use a for loop to iterate a fixed number of times

Prerequisites: "Why Loops Exist"

The three parts

for (Integer i = 0; i < 5; i++) {
    System.debug('Iteration number ' + i);
}
  1. for (Integer i = 0; i < 5; i++) { — three parts, separated by ;: Integer i = 0 runs once, before the loop starts; i < 5 is checked before every pass, and the loop stops the moment it's false; i++ runs after every pass, right before the condition is checked again.
  2. System.debug('Iteration number ' + i); — runs once per pass, with i holding a different value each time.
  3. } — closes the loop.

This prints 0 through 4 — five iterations, not six, because i < 5 becomes false once i reaches 5.

A real business example: Retail

Integer storeCount = 12;

for (Integer storeNumber = 1; storeNumber <= storeCount; storeNumber++) {
    System.debug('Generating end-of-day report for store #' + storeNumber);
}
  1. Integer storeCount = 12; — the number of stores.
  2. (blank line)
  3. for (Integer storeNumber = 1; storeNumber <= storeCount; storeNumber++) { — starts at 1 (not 0, since stores are naturally numbered from 1) and runs through storeCount inclusive, thanks to <=.
  4. System.debug('Generating end-of-day report for store #' + storeNumber); — runs once per store.
  5. } — closes the loop.

Whenever you know exactly how many times something needs to happen — here, once per store — a classic for loop is the clearest way to express it.

Common mistakes

  • Off-by-one errors. i <= 5 loops 6 times (0 through 5); i < 5 loops 5 times (0 through 4). Always double-check which one you actually mean.
  • Forgetting the increment. Without i++, the condition never changes and the loop never ends — Apex will eventually hit a governor limit and throw an error rather than freeze forever, but it's still a bug worth avoiding.

Counting down instead of up

for (Integer i = 5; i > 0; i--) {
    System.debug(i);
}
System.debug('Liftoff!');
  1. for (Integer i = 5; i > 0; i--) { — starts at 5, keeps going while i > 0, and i-- decrements by one each pass instead of counting up.
  2. System.debug(i); — logs 5, 4, 3, 2, then 1.
  3. } — closes the loop; it stops once i reaches 0.
  4. System.debug('Liftoff!'); — runs once, after the loop finishes.

A for loop is not limited to counting up by one — the increment step can be any expression, including a decrement, to count down instead.

Exercise

Write a for loop that debugs the numbers 1 through 10 (inclusive).

Show hint

Start i at 1 and use <= 10 as the condition.

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

For Loops Quiz

1. How many times does for (Integer i = 0; i < 5; i++) run?

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 classic for loop packs a counter's start, its stopping condition, and how it changes each pass into one line — the most explicit and controllable loop shape in Apex.