Beginner 15 min read

Break and Continue

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

  • Use break to exit a loop early
  • Use continue to skip to the next iteration
  • Explain the difference between the two

Prerequisites: "Do-While Loops"

break: stop the loop entirely

List<Integer> orderIds = new List<Integer>{101, 102, 103, 104, 105};

for (Integer orderId : orderIds) {
    if (orderId == 103) {
        System.debug('Found order 103, stopping search.');
        break;
    }
    System.debug('Checked order ' + orderId);
}
  1. List<Integer> orderIds = new List<Integer>{101, 102, 103, 104, 105}; — five order IDs.
  2. (blank line)
  3. for (Integer orderId : orderIds) { — checks each ID in turn.
  4. if (orderId == 103) { — true once orderId reaches 103.
  5. System.debug('Found order 103, stopping search.'); — runs at that point.
  6. break; — exits the loop immediately — 104 and 105 are never even checked.
  7. } — closes the if.
  8. System.debug('Checked order ' + orderId); — runs for every ID that didn't match line 4 (101 and 102).
  9. } — closes the loop.

This is useful once you've found what you were looking for and don't need to keep going.

continue: skip just this iteration

List<Integer> quantities = new List<Integer>{5, 0, 8, 0, 3};

for (Integer quantity : quantities) {
    if (quantity == 0) {
        continue;
    }
    System.debug('Processing quantity: ' + quantity);
}
  1. List<Integer> quantities = new List<Integer>{5, 0, 8, 0, 3}; — five quantities, including two zeros.
  2. (blank line)
  3. for (Integer quantity : quantities) { — checks each quantity in turn.
  4. if (quantity == 0) { — true for the two zero entries.
  5. continue; — skips only the rest of the current pass; the loop moves straight on to the next item instead of stopping altogether.
  6. } — closes the if.
  7. System.debug('Processing quantity: ' + quantity); — runs for every quantity that wasn't zero: 5, 8, and 3.
  8. } — closes the loop.

A real business example: HR (Candidate Screening)

List<Integer> candidateScores = new List<Integer>{45, 62, 88, 30, 91};

for (Integer score : candidateScores) {
    if (score < 50) {
        continue; // doesn't meet the minimum bar, skip to the next candidate
    }
    if (score >= 90) {
        System.debug('Score ' + score + ' — fast-track to final round.');
        break; // found a top candidate, no need to keep screening
    }
    System.debug('Score ' + score + ' — standard review.');
}
  1. List<Integer> candidateScores = new List<Integer>{45, 62, 88, 30, 91}; — five scores.
  2. (blank line)
  3. for (Integer score : candidateScores) { — checks each score in turn.
  4. if (score < 50) { — true for 45 and 30.
  5. continue; — skips those candidates entirely, moving to the next score.
  6. } — closes that if.
  7. if (score >= 90) { — true once score reaches 91.
  8. System.debug('Score ' + score + ' — fast-track to final round.'); — runs for that candidate.
  9. break; — stops screening entirely — no need to look at any candidates after this one.
  10. } — closes that if.
  11. System.debug('Score ' + score + ' — standard review.'); — runs for every score that cleared line 4 but didn't trigger line 7 (here, 62 and 88).
  12. } — closes the loop.

continue filters out candidates who don't clear a minimum bar; break stops screening entirely once a standout is found — both express "change the loop's normal path" but in different ways.

Exercise

Given List<Integer> numbers = new List<Integer>{2, 4, 7, 8, 10}, loop through and debug each number, but stop entirely (break) as soon as you hit the first odd number.

Show hint

Use Math.mod(number, 2) != 0 to check for odd, then break.

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Break and Continue Quiz

1. What does continue do?

2. break exits only the current iteration, leaving the loop running.

Log in to submit the quiz and save your score.

Leaderboard

  • 1. Monde Masiza 2/2

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

break exits a loop immediately; continue skips the rest of the current iteration and moves to the next one — two ways to override a loop's default "run every pass" behavior.