# Apex control flow, loops and collections

*Platform Developer Study Guide · Process Automation and Logic · Last updated October 15, 2026 · https://www.certifyforce.com/study-guide/platform-developer/process-automation-and-logic/apex-control-flow-and-collections*

How if-else, switch and the five Apex loops execute, what break and continue do, how SOQL for loops chunk records, how lists, sets and maps behave while you iterate them, and how the safe navigation and null coalescing operators handle nulls.

- Certification: Salesforce Platform Developer
- Exam section: Process Automation and Logic
- Study guide: https://www.certifyforce.com/study-guide/platform-developer

**Official exam objective:**

> Given a scenario, assess how Apex control flow statements will execute and when to apply different control flow statements.

## What the exam expects

- Predict which branch of an if-else chain or `switch on` runs, including `when else` and null.
- Choose among do-while, while, traditional for, list or set iteration and SOQL for loops, and predict how many times each body runs.
- Tell `break` from `continue` and `return` in a loop.
- Iterate lists, sets and maps safely, and pick the collection whose rules (order, duplicates, keys) fit the job.
- Use the safe navigation (`?.`) and null coalescing (`??`) operators to avoid a NullPointerException.

Key terms: **when else** (the switch default block); **do-while** (tests its condition after the body); **SOQL for loop** (iterates query results in chunks); **safe navigation** (`?.` returns null instead of throwing); **null coalescing** (`??` supplies a fallback for null).

## Key facts

| Fact | Detail | Source |
|---|---|---|
| switch expression types | Integer, Long, sObject, String, Enum | [Switch Statements](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_switch.htm) |
| switch fall-through | None; `when else` is optional and must be last | [Switch Statements](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_switch.htm) |
| do-while | Body always runs at least once | [Do-While Loops](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_do_while.htm) |
| while | Condition tested first; body may never run | [While Loops](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_while.htm) |
| Loop types | Five: do-while, while, for, list or set iteration, SOQL for | [Loops](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops.htm) |
| break / continue | Exits the entire loop / skips to the next iteration | [Loops](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops.htm) |
| SOQL for loop, list format | Body runs once per 200 records | [SOQL For Loops](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_for_SOQL.htm) |
| Modifying a collection mid-iteration | Not supported; causes an error | [Iterating Collections](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_iterating.htm) |
| Map String keys | Case-sensitive; an existing key is overwritten by put | [Maps](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_maps.htm) |
| `?.` on null | Returns null; right side not evaluated | [Safe Navigation Operator](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SafeNavigationOperator.htm) |

## Check yourself

- When does a do-while body run even though its condition is false?
- Why use the list format of a SOQL for loop when the loop body performs DML?
- What happens when no `when` value matches and there is no `when else`?
- Why would you choose a set over a list for a group of Ids?
- What does `con.Account?.BillingCity` return when the contact has no account?

## Sources

- [Control Flow Statements](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_control_flow.htm)
- [Do-While Loops](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_do_while.htm)
- [While Loops](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_while.htm)
- [Loops](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops.htm)
- [For Loops](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_for.htm)
- [SOQL For Loops](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_for_SOQL.htm)
- [Switch Statements](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_switch.htm)
- [Conditional (If-Else) Statements](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_if_else.htm)
- [List or Set Iteration for Loops](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_for_lists.htm)
- [Iterating Collections](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_iterating.htm)
- [Lists](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_lists.htm)
- [Sets](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_sets.htm)
- [Maps](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_maps.htm)
- [Safe Navigation Operator](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SafeNavigationOperator.htm)
- [Null Coalescing Operator](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_NullCoalescingOperator.htm)
- [Expression Operators](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_expressions_operators_understanding.htm)

## About the full unit

The "Core concepts", "Exam traps" and "Worked scenario" sections of this unit, its 8 linked practice questions, and unlimited timed mock exams are included with the Platform Developer pack (USD $19): https://www.certifyforce.com/practice-exams
