Study for the Salesforce Platform Developer Exam. Study with flashcards and multiple choice questions, each question has hints and explanations. Get ready for your exam!

Practice this question and more.


When the number of records in a recordset is unknown, which control statement should be used to execute code for every record?

  1. For (init_stmt, exit_condition; increment_stmt) { }

  2. Do { } While (Condition)

  3. For (variable : list_or_set) { }

  4. While (Condition) { ... }

The correct answer is: For (variable : list_or_set) { }

The correct approach to handle a situation where the number of records in a recordset is unknown is to use a control statement that iterates through each record without requiring a predefined limit. The option that enables this type of iteration is the one that uses the syntax of "For (variable : list_or_set) { }". This control structure is specifically designed for collections such as lists and sets, which can dynamically change in size and do not have a known count at compile time. By using this syntax, developers can seamlessly loop through every element within the collection, ensuring that all records in the recordset are processed, regardless of the number. The other control statements, including the traditional for loop and while loop, are generally more suited for scenarios where an upper limit is known or requires a specific exit condition. For instance, a while loop evaluates a condition before each iteration and may not be ideal if you are uncertain whether there are more records to process. Similarly, an increment-based for loop needs an initial count and defines a clear termination condition, which does not fit with collections that might change in size or have an indeterminate number of elements. Hence, the syntax provided in the correct choice provides an efficient and straightforward method for iterating through all records without