Which loop is most commonly used to traverse every element of a fixed-size C++ array using its index?
Options
- A. for loop
- B. Only switch statement
- C. Only goto statement
- D. More than one of the above
- E. None of the above
Quiz Practice:
A. for loop
Answer: (A) for loop
A for loop is commonly used to traverse an array because the array indexes follow a predictable sequence.
For example:
int a[5] = {1, 2, 3, 4, 5};
for(int i = 0; i < 5; i++)
{
cout << a[i];
}
The loop accesses indexes from 0 through 4.