What's the "correct" way to write a decreasing loop with a size_t value and a boundary condition. Example incorrect implementation:
for (size_t elemNum = listSize-1; elemNum >= 0; --elemNum) { /* ... */ }
When it reaches zero it will wrap around to the max value rather than acting as a boundary condition. Iterating the loop in reverse is necessary. It seems like a problem that would have a defacto standard solution but I can't find what it is.
The most succinct approach is to use post-increment:
for (size_t i = listSize; i--;) ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With