C looping statement
Definition
A loop is a way to repeat a block of code again and again until a certain condition is false.
In C, looping (or iteration) statements let you execute a block of code repeatedly as long as a condition holds.
Looping allows a set of instructions to be executed repeatedly until a specific condition is met. C provides three distinct looping constructs: `for`, `while`, and `do...while`, each suited for different control flow scenarios.
1. for Loop
A for loop is a control structure that allows code to be executed repeatedly based on a condition. The `for` loop combines initialization, condition evaluation, and update expressions all within a single statement, offering compact control over iteration.
Syntax:
for (initialization; condition; update) {
// body: runs each iteration
} - Initialization runs once before the loop starts (e.g. int i = 0;).
- Before each iteration, the loop evaluates the condition—executing the body only if the condition holds true.
- Update executes after each iteration (e.g. i++).
Example
#include <stdio.h>
int main(void) {
for (int i = 1; i <= 5; i++) {
printf("%d ", i);
}
// Output: 1 2 3 4 5
return 0;
} 2. while Loop
A while loop is a control structure that executes a block of code as long as the condition is true. It checks the condition before each iteration.
Syntax:
while (condition) {
// body: runs as long as condition is true
}- The condition is checked before each iteration.
- If the condition is false at the start, the body may never execute.
Example
#include <stdio.h>
int main(void) {
int n = 5;
while (n > 0) {
printf("%d ", n);
n--;
}
// Output: 5 4 3 2 1
return 0;
} 3. do...while Loop
The do...while loop differs by executing the code block first, then deciding whether to continue based on the condition—ensuring one guaranteed pass.
Syntax:
do {
// body: always runs at least once
} while (condition); - The loop body runs initially without any condition, and only then the condition is evaluated for subsequent iterations.
- Useful when you need the loop body to run at least one time.
Example
#include <stdio.h>
int main(void) {
int n = 1;
do {
printf("Enter a number (0 to quit): ");
scanf("%d", &n);
printf("You entered: %d\n", n);
} while (n != 0);
return 0;
} 4. Controlling Loop Execution
- break: immediately exits the nearest enclosing loop.
- continue: skips the rest of the current iteration and jumps to the next.
Example with break and continue
#include <stdio.h>
int main(void) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue; // skip printing 5
}
if (i == 8) {
break; // exit loop when i == 8
}
printf("%d ", i);
}
// Output: 1 2 3 4 6 7
return 0;
} 5. Nested Loops
You can place one loop inside another. This is common for working with multi-dimensional data.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
printf("(%d,%d) ", i, j);
}
printf("\n");
} Notes
- Always ensure loops terminate (avoid infinite loops) unless intentionally infinite (e.g., event loops).
- Keep loop bodies simple; move complex logic into functions.
- For performance, minimize work inside the condition or update expressions.
Prefer Learning by Watching?
Watch these YouTube tutorials to understand C Tutorial visually:
What You'll Learn:
- 📌 C_33 Introduction to Loop in C Language | Need of loops| C Language Tutorials
- 📌 for and while Loops