Comprehensive and Detailed Explanation From Exact Extract:
To sum all numbers in an array, the program must iterate through each element exactly once and add it to a running total. According to foundational programming principles (e.g., Certiport Scripting and Programming Foundations Study Guide), a for loop is the most appropriate control structure for iterating over a known sequence like an array.
Task Analysis:
Input: An array of numbers (e.g., [1, 2, 3]).
Operation: Iterate through each element and add to a sum (e.g., 1 + 2 + 3 = 6).
A single loop is sufficient, as the task involves a single pass through the array.
Option A: "One while loop." This is incorrect. While a while loop can iterate through an array (e.g., using an index variable), it requires manual index management (e.g., i = 0; while i < length), making it less concise and more error-prone than a for loop for this task.
Option B: "Nested for loops." This is incorrect. Nested loops are used for multi-dimensional arrays or multiple iterations (e.g., matrix operations), but summing a single array requires only one loop.
Option C: "Multiple if statements." This is incorrect. If statements are for conditional logic, not iteration. They cannot traverse an array to sum elements.
Option D: "One for loop." This is correct. A for loop is ideal for iterating over an array’s elements. For example, in Python:
numbers = [1, 2, 3]
total = 0
for num in numbers:
total += num
Or in C:
c
int numbers[] = {1, 2, 3};
int total = 0;
for (int i = 0; i < 3; i++) {
total += numbers[i];
}
Certiport Scripting and Programming Foundations Study Guide (Section on Control Structures: Loops).
Python Documentation: “For Statements” (https://docs.python.org/3/tutorial/controlflow.html#for-statements).
W3Schools: “C For Loop” (https://www.w3schools.com/c/c_for_loop.php).