SkillByAIOpen interactive version →

Lesson 9 / 38

Loops

Repeat code with for, while and do-while loops and control them with break and continue.

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?

  • do ... while
  • while
  • for
  • switch
Answer

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