Here’s the thing: instead of writing the same code again and again, a for loop lets you automate repetition cleanly and efficiently.
In this article, you will learn the syntax, working, examples, and common use cases of the for loop in C.
Table of Contents
What is a For Loop in C?
A for loop is an entry-controlled loop that executes a block of code repeatedly based on a condition. It combines initialization, condition checking, and update in a single line, making it compact and easy to read.Syntax:
Explanation:for (initialization; condition; update) {
// code to be executed
}
- Initialization: This runs once at the beginning and is used to declare and initialize variables.
- Condition: The loop continues as long as this condition is true.
- Update: This updates the loop variable after each iteration.
- Initialization is executed once.
- Condition is checked.
- If true, the loop body runs.
- Update statement executes.
- The process repeats until the condition becomes false.
Example 1: Simple For Loop
Below is the C program for simple for loop:
// C program for simple loop
#include stdio.h
int main()
{
int i;
for (i = 1; i = 5; i++)
{
printf("%d\n", i);
}
return 0;
}
Output:
Explanation:1
2
3
4
5
The loop starts from 1 and runs until 5. After each iteration, the value of i increases by 1. When i becomes 6, the condition fails and the loop stops.
Example 2: Sum of First N Numbers
Below is the C program to find the sum of first N numbers:
#include stdio.h
int main() {
int i, n = 5, sum = 0;
for (i = 1; i = n; i++) {
sum = sum + i;
}
printf("Sum = %d", sum);
return 0;
}
Output:
Explanation:Sum = 15
The loop runs from 1 to 5 and keeps adding each value to the variable sum. By the end, it stores the total.
Example 3: Reverse Counting
Below is the C program for reverse counting:
// C program for reverse counting
#include stdio.h
int main()
{
int i;
for (i = 5; i = 1; i--)
{
printf("%d\n", i);
}
return 0;
}
Output:
Explanation:5
4
3
2
1
This loop starts from 5 and decreases the value of i after each iteration until it reaches 1.
Nested For Loop in C
Below is the C program for nested for loop:
// C program for nested for loop
#include stdio.h
int main()
{
int i, j;
for (i = 1; i = 3; i++)
{
for (j = 1; j = 2; j++)
{
printf("i = %d, j = %d\n", i, j);
}
}
return 0;
}
Output:
Explanation:i = 1, j = 1
i = 1, j = 2
i = 2, j = 1
i = 2, j = 2
i = 3, j = 1
i = 3, j = 2
A nested for loop means one loop inside another. The inner loop runs completely for each iteration of the outer loop.
Common Mistakes to Avoid
- Missing Semicolon: Forgetting semicolons inside the loop declaration can cause syntax errors.
- Infinite Loop: If the condition never becomes false, the loop runs forever.
- Wrong Update: Incorrect increment or decrement can lead to unexpected results.
- Off-by-One Error: Using instead of = (or vice versa) may give incorrect iteration counts.
- Variable Scope Issues: Declaring variables inside the loop incorrectly can limit their usage.
Conclusion
The for loop in C is a powerful and compact way to handle repetitive tasks. It combines initialization, condition checking, and updating in a single statement, making your code cleaner and easier to maintain.Once you understand how it flows, you can use it in everything from simple counting to complex logic.
Frequently Asked Questions
1. What is a for loop in C?2. When should I use a for loop?A for loop is a control structure used to execute a block of code multiple times based on a condition.
3. Can a for loop run infinitely?You should use a for loop when you know the number of iterations in advance.
4. Can I use multiple variables in a for loop?Yes, if the condition never becomes false, the loop will run indefinitely.
5. What is a nested for loop?Yes, you can initialize and update multiple variables within a for loop.
A nested for loop is a loop inside another loop, often used for multidimensional problems.
0 Comments