I've seen most people use a for instead of a while loop:
I find the following reasons to prefer the first over the second option:
- The statement for the for loop is too large and usually need to be split into 2 (ugly) or 3 (same as while) lines.
- In the while option, operations are performed on the variable Object instead of the iterator itElement, yielding a cleaner and more readable coding style. This becomes a greater advantage when the container is a map instead of a vector.
- If some elements must be removed from the container, the for loop is simply not an option, whereas the while option is the way to go.