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

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.