How to use loops
Count with for
A for loop keeps initialization, condition, and update together. Arrays normally use a size_t index.
for (size_t i = 0; i < count; ++i) {
printf("%zu\n", i);
}
Repeat with while
A while checks before each iteration. Ensure some path changes the condition or exits.
int remaining = 3;
while (remaining > 0) {
printf("%d\n", remaining);
--remaining;
}
Run once with do-while
A do/while checks after the body, so the body always executes at least once.
do {
printf("Enter 0 to quit: ");
} while (scanf("%d", &choice) == 1 && choice != 0);
Keep bounds correct
Use i < length for zero-based arrays. An off-by-one index can read or write outside the array and cause undefined behavior.