Loop Patterns: for, while, do-while
Iterate with the three classic loop forms and pick the right one for the situation.
The for Loop
The classic indexed loop. Use it when you know how many iterations you need.
for (int i = 0; i < 10; ++i) {
std::cout << i << " ";
}Anatomy of a for Loop
Three parts separated by semicolons:
- Init — runs once before the loop
- Condition — checked before each iteration
- Update — runs after each iteration
Any part can be empty.
All lessons in this course
- Branching: if-else, ternary, switch-case
- Loop Patterns: for, while, do-while
- Range-Based for Loop with Containers
- Early Returns break continue and goto