Skip to content
Justice Tech School

for, while and do while

for repeats a known number of times, while checks first and do while runs once at least.

Example
#include <stdio.h>

int main(void) {
  for (int i = 0; i < 3; i++) printf("%d ", i);
  int n = 3;
  while (n--) printf("w%d ", n);
  do { printf("once"); } while (0);
  printf("\n");
  return 0;
}

Result

0 1 2 w2 w1 w0 once

Exercise

Run once at least.

 { } while (0);

Quiz Test

Which checks after the body?

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