Module 2 · Lesson 2 of 35/26 in the course~14 min
Module lessons (2/3)
Iterative cycles
Lesson preview5 hands-on exercises readyCourse C++ · Module 2 — Flow Control
Repeat blocks of code with for loops, while, and do-while.
What you'll learn in this lesson
- Choose confidently between
for(known number of steps),while(pre-test condition) anddo-while(at least one execution). - Iterate through entire collections without manual indexes with range-based for (
for (const auto& item : items)). - Control interrupts (
break) and iteration skips (continue) avoiding infinite loops and off-by-one errors.
Overview
Loops in C++ are compiled into direct machine jumps without any overhead of iterator objects allocated in the Heap. Knowing how to choose the clearest construct makes the code immediate and reduces the risk of exceeding the limits of an array.
When you know the starting point and increment step, for concentrates initialization, guard and update in one line:
Sections covered in this lesson
- 11. The Classical
forCycle - 22.
whilevsdo-while - 33. Range-Based
for(C++11+) - 44.
breakandcontinue - 5Practical exercises
- 6Check for understanding
Pro Lesson · Module 2 — Flow Control
Unlock "Iterative cycles" and the full Course C++ curriculum
Repeat blocks of code with for loops, while, and do-while.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Course C++ plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.