Lesson 9 / 36

Loops

Repeat a block while a condition holds.

while / do-while / for

do ... while runs the body at least once. for is best when you know the count.

for (int i = 1; i <= 5; i++) {
    printf("%d ", i);
}
// 1 2 3 4 5

Output:

1 2 3 4 5

break & continue

break exits the loop entirely. continue skips to the next iteration.

Quick check: Which loop always runs its body at least once?

  • while
  • for
  • do ... while
Answer

do ... while — do ... while checks its condition after the body.