Salesforce Platform Developer I (legacy) — Question 243
Which control statement should a developer use to ensure that a loop body executes at least once?
Answer options
- A. for (init_stmt; exit_condition; increment_stmt) {ג€¦}
- B. do {ג€¦} while (condition)
- C. while (condition) {ג€¦}
- D. for (variable : list_or_set) {ג€¦}
Correct answer: B
Explanation
The correct answer is B, as the 'do...while' loop ensures that the body executes first and then checks the condition, allowing for at least one execution. In contrast, options A and C check the condition before executing the loop body, which could result in no executions if the condition is false initially. Option D is a for-each loop that also does not guarantee execution if the list or set is empty.