for Loop in C
The for loop is the most used loop in C. It is perfect when you know how many times to repeat: print 1 to 10, add 100 numbers, go through every element of an array.
In this lesson you will learn its syntax and flowchart, and most importantly how to do a dry run: tracing the loop step by step on paper, exactly as exams and interviews expect.
What You Will Learn
- The syntax and flowchart of the for loop.
- The order in which its three parts run.
- How to do a dry run with a table.
- Sum of N numbers and factorial programs.
- Useful variations: counting down, steps of 2, empty parts.
for Loop in C Explained
Exam definition: The for loop is an entry-controlled loop that keeps the initialization, condition and update in one line. It is mainly used when the number of iterations is known.
Memory trick: The for loop is like a running track: start line (initialization), "Are you at the finish?" (condition), run a lap (body), step forward (update). The start line is crossed only once.
Syntax of for Loop in C
for (initialization; condition; update)
{
/* body: repeated while condition is true */
}
Order of execution
- Initialization: runs only once, at the very start.
- Condition: checked before every iteration. If false, the loop ends.
- Body: runs if the condition is true.
- Update: runs after the body, then go back to step 2.
Flowchart of the for loop
[ initialization ]
|
v
+--> < condition? > --No--> [ exit loop ]
| | Yes
| v
| [ body ]
| |
| v
+---- [ update ]
Example 1: Sum of First N Natural Numbers Using for Loop
#include <stdio.h>
int main(void)
{
int n, sum = 0;
printf("Enter N: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
sum = sum + i;
}
printf("Sum of 1 to %d = %d\n", n, sum);
return 0;
}
Output
Enter N: 5 Sum of 1 to 5 = 15
Dry Run of the for Loop (N = 5)
A dry run means running the program on paper, writing the value of every variable at each step:
| Iteration | i | Condition i ≤ 5 | sum = sum + i |
|---|---|---|---|
| 1 | 1 | True | 0 + 1 = 1 |
| 2 | 2 | True | 1 + 2 = 3 |
| 3 | 3 | True | 3 + 3 = 6 |
| 4 | 4 | True | 6 + 4 = 10 |
| 5 | 5 | True | 10 + 5 = 15 |
| – | 6 | False → exit | sum stays 15 |
The loop body ran 5 times, but the condition was checked 6 times: the last check (i = 6) is the one that stops the loop.
How the for Loop Works
int i = 1runs once. (Declaringiinside the for is allowed since C99; theniexists only inside the loop.)- Before every iteration,
i <= nis checked. - After every iteration,
i++moves i one step closer to the end. - For a loop
for (i = a; i <= b; i++), the body runs b − a + 1 times.
Example 2: Factorial of a Number Using for Loop
#include <stdio.h>
int main(void)
{
int n;
long long fact = 1;
printf("Enter a number: ");
scanf("%d", &n);
for (int i = n; i >= 1; i--) /* counting down */
fact = fact * i;
printf("%d! = %lld\n", n, fact);
printf("Odd numbers up to 9: ");
for (int k = 1; k <= 9; k += 2) /* step of 2 */
printf("%d ", k);
printf("\n");
return 0;
}
Output
Enter a number: 5 5! = 120 Odd numbers up to 9: 1 3 5 7 9
5! = 5 × 4 × 3 × 2 × 1 = 120. The loop counts down with i--; the second loop jumps in steps of 2 with k += 2. For more practice, see our C program to display a multiplication table.
Important Points to Remember
- Order: init (once) → condition → body → update → condition ...
- for is entry-controlled: it may run 0 times.
- Best when the number of iterations is known.
- All three parts are optional:
for (;;)is an infinite loop. - No semicolon right after
for (...).
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
for (i = 0; i < 5; i++); | Empty loop; body runs once after | Remove the semicolon |
Commas instead of semicolons: for (i = 0, i < 5, i++) | Syntax error | Use ; between the parts |
for (i = 1; i < 10; i--) | Infinite loop (i moves away from the end) | Update in the right direction |
Not initializing sum | Garbage result | int sum = 0; |
Factorial in an int for big numbers | Overflow (13! doesn't fit) | Use long long |
for Loop Practice Questions
- Write the syntax of the for loop and the order in which its parts run.
- How many times does the initialization part run?
- How many times does
for (i = 3; i <= 9; i++)run? - Do a dry run of
for (i = 1; i <= 4; i++) p = p * 2;withp = 1. - What does
for (;;)do?
Show answers
for (init; condition; update) { body }. Order: init once, then condition → body → update repeatedly.- Only once.
- 7 times (9 − 3 + 1).
- i=1: p=2; i=2: p=4; i=3: p=8; i=4: p=16; i=5: exit. Final p = 16.
- It is an infinite loop (no condition means always true).
Coding Exercise
Read N and print the sum of the squares of 1 to N (1² + 2² + ... + N²). Test with N = 4 (answer 30).
Show solution
#include <stdio.h>
int main(void)
{
int n, sum = 0;
printf("Enter N: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++)
sum += i * i;
printf("Sum of squares = %d\n", sum);
return 0;
}
Enter N: 4 Sum of squares = 30
Quick Quiz: for Loop in C (MCQ)
-
In a for loop, which part runs only once?
A) Condition B) Update C) Initialization D) Body
-
How many times is "Hi" printed?
for (i = 0; i < 3; i++) printf("Hi");A) 2 B) 3 C) 4 D) 0
-
Which is an infinite loop?
A)
for (i = 0; i < 5; i++)B)for (;;)C)for (i = 5; i > 0; i--)D) None -
Output of
for (i = 1; i <= 3; i++); printf("%d", i);A) 123 B) 3 C) 4 D) Error
-
The for loop is:
A) Exit-controlled B) Entry-controlled C) Not a loop D) Recursive
Show answers
- C: initialization.
- B: 3.
- B:
for (;;) - C: 4 (the semicolon makes an empty loop; after it, i is 4).
- B: entry-controlled.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Syntax | for (init; condition; update) { body } |
|---|---|
| Order | Init once → condition → body → update → repeat |
| Type | Entry-controlled |
| Best for | Known number of iterations |
| Iterations of i = a..b | b − a + 1 |
| Infinite for | for (;;) |
| 5! | 120 |
Frequently asked questions
- What is a dry run?
- Running the program on paper, writing the value of every variable at each step.
- Are all three parts of a for loop compulsory?
- No. All are optional, but the two semicolons are required;
for (;;)loops forever. - How many times is the condition checked in a loop that runs n times?
- n + 1 times: the last check is false and ends the loop.
- Can we declare the loop variable inside for?
- Yes, since C99:
for (int i = 0; ...); then i exists only inside the loop.
Remember
- for = running track: start once, check, lap, step.
- Body runs n times, condition checked n + 1 times.
Next Topic
Next lesson: while Loop in C. You will learn the while loop and use it to reverse a number.