Skip to content
Justice Tech School

Loops

while checks first, do...while runs once before checking, and for packs start, condition and step in one line. break leaves a loop and continue skips to the next round.

Example
for (int i = 1; i <= 3; i++) {
  printf("i = %d\n", i);
}
int n = 3;
while (n > 0) {
  printf("n = %d\n", n);
  n--;
}
for (int i = 1; i <= 6; i++) {
  if (i == 2) continue;
  if (i == 5) break;
  printf("%d ", i);
}
printf("\n");

Result

i = 1
i = 2
i = 3
n = 3
n = 2
n = 1
1 3 4 

Exercise

Repeat while the condition holds.

 (n > 0) { n--; }

Quiz Test

What does continue do?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.