Loops
Loops let you repeat a block of code multiple times without rewriting it. C provides three loop types: for (best when you know how many times to repeat), while (repeats while a condition holds), and do-while (always runs at least once).
💡 Key insight
A do-while loop checks its condition after the loop body runs — so the body always executes at least one time.
Syntax
syntax
for (init; condition; update) { }
while (condition) { }
do { } while (condition);Example
The loop runs while i <= 5, incrementing i by 1 after each pass.
c
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}output1 2 3 4 5
Try it yourself
Copy this example into the C compiler and run it — try changing a value and see what happens.
