Table of Contents
What Is a for Loop in C?
A for loop in C is a control structure that allows you to execute a block of code a fixed number of times. Instead of writing the same code repeatedly you can tell the computer:
“Do this task n times.”
Syntax of for Loop in C
for (initialization; condition; update) {
// code to be executed
}
Breakdown of Each Part
Initialization: It sets the starting value of the loop variable and executes only once at the very beginning of the loop.
Condition: It evaluates a boolean expression before every iteration to determine whether the loop should continue running or terminate.
Update: It modifies the loop variable (increment or decrement) after the loop body executes to prepare for the next condition check.
Simple Example: Write a C program to print Numbers from 1 to 5
#include
int main() {
int i;
for (i = 1; i <= 5; i++) {
printf("%d ", i);
}
return 0;
}
// 1 2 3 4 5
| Value of i | check i <= 5 | Output | Updates |
|---|---|---|---|
| 1 | True | 1 | i is incremented to 2 |
| 2 | True | 2 | i is incremented to 3 |
| 3 | True | 3 | i is incremented to 4 |
| 4 | True | 4 | i is incremented to 5 |
| 5 | True | 5 | i is incremented to 6 |
| 6 | False | - | terminates the loop |

Why Use a for Loop?
The for loop is perfect when:
- You know how many times the loop should run
- You want clean and readable code
- You are working with arrays, strings or patterns
Write a C program to print the sum of n numbers using the for loop
#include
int main() {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
printf("Sum = %d", sum);
return 0;
}
| value of i | check (i <= 10) | (sum + i) | Sum | Increment |
|---|---|---|---|---|
| 0 | Enter Loop | |||
| 1 | True | 0 + 1 | 1 | i is incremented to 2 |
| 2 | True | 1 + 2 | 3 | i is incremented to 3 |
| 3 | True | 3 + 3 | 6 | i is incremented to 4 |
| 4 | True | 6 + 4 | 10 | i is incremented to 5 |
| 5 | True | 10 + 5 | 15 | i is incremented to 6 |
| 6 | True | 15 + 6 | 21 | i is incremented to 7 |
| 7 | True | 21 + 7 | 28 | i is incremented to 8 |
| 8 | True | 28 + 8 | 36 | i is incremented to 9 |
| 9 | True | 36 + 9 | 45 | i is incremented to 10 |
| 10 | True | 45 + 10 | 55 | i is incremented to 11 |
| 11 | False | 55 | Loop ends |
Write a C program to print the Fibonacci Series by using for loop
#include
int main() {
int n= 10, a = 0, b = 1, c;
printf("Fibonacci Series: ");
for (int i = 1; i <= n; i++) {
printf("%d ", a);
c = a + b;
a = b;
b = c;
}
return 0;
}
// Fibonacci Series: 0 1 1 2 3 5 8 13 21 34
| i | a (Printed) | b | c = a + b | New a | New b |
|---|---|---|---|---|---|
| 1 | 0 | 1 | 1 | 1 | 1 |
| 2 | 1 | 1 | 2 | 1 | 2 |
| 3 | 1 | 2 | 3 | 2 | 3 |
| 4 | 2 | 3 | 5 | 3 | 5 |
| 5 | 3 | 5 | 8 | 5 | 8 |
Nested for Loop (Loop Inside a Loop)
Placing one loop inside another is known as a nested loop. For every single iteration of the outer loop the inner loop runs through its entire cycle from beginning to end.
#include
int main() {
int i, j;
for (i = 1; i <= 4; i++) {
for (j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
// 1
// 1 2
// 1 2 3
// 1 2 3 4
| i (Row) | j (Col) | Check ( j <= i ) | Output | Visual Output (Current Line) |
|---|---|---|---|---|
| 1 | 1 | True | 1 | 1 |
| 1 | 2 | False | \n | (end of line) |
| 2 | 1 | True | 1 | 1 |
| 2 | 2 | True | 2 | 1 2 |
| 2 | 3 | False | \n | (end of line) |
| 3 | 1 | True | 1 | 1 |
| 3 | 2 | True | 2 | 1 2 |
| 3 | 3 | True | 3 | 1 2 3 |
| 3 | 4 | False | \n | (end of line) |
| 4 | 1 | True | 1 | 1 |
| 4 | 2 | True | 2 | 1 2 |
| 4 | 3 | True | 3 | 1 2 3 |
| 4 | 4 | True | 4 | 1 2 3 4 |
| 4 | 5 | False | \n | (end of line) |
| 5 | Loop ends |
Applications
1) Repeated Execution of Statements: This looping structure is used to execute a set of instructions multiple times when the number of repetitions is already known. It helps reduce code length and improves program efficiency.
2) Array and String Traversal: It is widely used to access each element of an array or each character of a string in sequence. This makes operations like searching, sorting, and updating data easier and faster.
3) Mathematical and Logical Calculations: This structure is useful in performing calculations such as sum of numbers, factorial, prime number checking and Fibonacci series by repeating arithmetic operations.
4) Pattern Generation: It is used to generate different patterns such as stars, numbers and pyramids. Pattern programs help improve logical thinking and understanding of nested repetition.
5) Data Processing and Analysis: In real-world applications, it processes large amounts of data such as student marks, sales records, and survey information to calculate totals, averages and other statistical values.
6) Game and Graphics Development: It plays an important role in controlling animations, character movement and drawing grids or frames by updating object positions repeatedly.
7) Embedded Systems and Hardware Control: In embedded systems, it is used to control hardware components such as LEDs, motors and sensors by repeating operations at fixed intervals.
Frequently Asked Questions ( FAQs)
1) What is the syntax of C for loop?
The syntax of a for loop in C is:
for (initialization; condition; update) {
// statements
}
It allows a block of code to run repeatedly until the condition becomes false.
2) What is a nested for loop in C?
A nested for loop is a for loop written inside another for loop.
It is commonly used for patterns, matrices, and table generation.
3) Can a for loop run without a condition?
Yes. If the condition is omitted, the for loop becomes an infinite loop and runs continuously unless terminated using break.