C++ - For Loop
Overview
Estimated time: 25–35 minutes
Iterate with classic for and range-based for loops. Learn indices vs iterators and nesting.
Learning Objectives
- Write counting loops and iterate ranges.
- Use range-based for for container traversal.
Prerequisites
Counting loop
for (int i=0;i<5;++i) { /*...*/ }
Range-based for
#include
for (int x : std::vector{1,2,3}) { /*...*/ }
Common Pitfalls
- Modifying a container while iterating can invalidate iterators.