C loops repeat a statement while an explicit condition remains true. Choose for when initialization, test, and update form one counting rule; while for condition-driven repetition; and do-while when the body must execute once before testing.
A for loop groups initialization, continuation test, and update. The test runs before each iteration. Keep the counter type compatible with the bound and write the bound to match the valid index range: an array of count elements uses indexes 0 through count - 1.
Declare a loop variable inside the header when it has no purpose afterward. Avoid modifying the same counter in both the update expression and body because it obscures which values are visited.
A while loop fits sentinel input, linked traversal, retries, and parsers where the number of iterations is not known in advance. Establish every value used by the condition before entering the loop, and make progress toward termination on every normal path.
A do-while executes the body first and tests afterward. It is suitable for menus or validation prompts that must run once. It is not a substitute for duplicating initialization when zero iterations are a valid outcome.
break exits the nearest loop; continue skips to its next iteration. Use them when they make the termination rule clearer, and remember that neither automatically exits an outer loop. A flag, function return, or refactored helper is often clearer for multi-level exit.
For nested loops, count total work across both dimensions. Two independent loops may be O(n + m), while a full n-by-m pair traversal is O(nm). Reset inner-loop state at the correct level.
Before coding, state the invariant: what is already processed at the start of each iteration. Check empty input, one element, the final valid index, and a bound near the type limit. Unsigned countdown loops require care because zero minus one wraps to a large value.
A loop must either terminate or be intentionally infinite with a documented external stop. In input loops, handle EOF and conversion failure; retrying forever without consuming invalid input is a common bug.
#include <stddef.h>
#include <stdio.h>
int main(void) {
int values[] = {4, 8, 15, 16, 23, 42};
size_t count = sizeof values / sizeof values[0];
long total = 0;
for (size_t i = 0; i < count; ++i) {
total += values[i];
}
printf("total=%ld\n", total);
}
#include <stdio.h>
int main(void) {
int value;
while (printf("Positive integer (0 quits): "), scanf("%d", &value) == 1) {
if (value == 0) break;
if (value < 0) {
puts("Negative values are ignored.");
continue;
}
printf("square=%d\n", value * value);
}
}
Use for for a compact counting rule, while for condition-driven repetition, and do-while only when one execution before the test is required.
Unsigned values cannot become negative. Decrementing zero wraps, so a condition such as i >= 0 never becomes false.
Practice, interview questions, and compiler links for C Loops.
Finish the concept here, then reinforce it with hands-on coding, interview prep, or a tool that matches the topic.
Explore 500+ free tutorials across 20+ languages and frameworks.
Fresh tutorials, interview guides, and coding practice in your inbox.