C Notes
Master the for loop in C with syntax, flowchart, variations, nested loops, loop patterns, and practical examples. Complete guide with interview questions.
The for loop is the most commonly used loop in C programming. It's a compact, elegant structure that puts initialization, condition, and update all in one line. When you know exactly how many times you want to repeat something — printing a table, processing array elements, or generating patterns — the for loop is your go-to choice.
The beauty of the for loop lies in its clarity. One glance at the loop header tells you where it starts, when it stops, and how it progresses. This makes code easier to read, write, and debug compared to equivalent while loops.
Syntax of For Loop
for (initialization; condition; update) {
// loop body
}| Part | Purpose | Example |
|---|---|---|
| Initialization | Executed once before the loop starts | i = 0 |
| Condition | Checked before each iteration | i < 10 |
| Update | Executed after each iteration | i++ |
| Body | Code that repeats | printf(...) |
Execution Order
- Initialization executes (only once)
- Condition is evaluated
- If true → execute body, then update, go to step 2
- If false → exit the loop
Flowchart of For Loop
Basic Examples
Example 1: Print Numbers 1 to 10
1 2 3 4 5 6 7 8 9 10
Example 2: Sum of First N Natural Numbers
Enter N: 100 Sum of first 100 natural numbers = 5050
Example 3: Multiplication Table
Enter a number: 7 Multiplication table of 7: 7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70
Variations of For Loop
Counting Backwards
for (int i = 10; i >= 1; i--) {
printf("%d ", i);
}
// Output: 10 9 8 7 6 5 4 3 2 1Step Size Other Than 1
// Count by 2s (even numbers)
for (int i = 2; i <= 20; i += 2) {
printf("%d ", i);
}
// Output: 2 4 6 8 10 12 14 16 18 20Multiple Variables
i=0, j=10 i=1, j=9 i=2, j=8 i=3, j=7 i=4, j=6
Omitting Parts of For Loop
All three parts of a for loop are optional:
Nested For Loops
When you place one for loop inside another, you get a nested loop. The inner loop completes all its iterations for each single iteration of the outer loop.
Example: Number Pattern
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Example: Star Pyramid
* *** ***** ******* *********
Example: Matrix Multiplication Pattern
3x3 Multiplication Matrix:
1 2 3 4 5
--------------------
1 | 1 2 3 4 5
2 | 2 4 6 8 10
3 | 3 6 9 12 15
4 | 4 8 12 16 20
5 | 5 10 15 20 25Practical Examples
Factorial Calculation
Enter a number: 10 10! = 3628800
Check Prime Number
Enter a number: 29 29 is a prime number.
Array Traversal
Array elements: 10 25 30 45 50 65 70 Sum = 295 Average = 42.14
Common Loop Patterns
| Pattern | Code | Output |
|---|---|---|
| Count up | for(i=1; i<=5; i++) | 1 2 3 4 5 |
| Count down | for(i=5; i>=1; i--) | 5 4 3 2 1 |
| Even numbers | for(i=2; i<=10; i+=2) | 2 4 6 8 10 |
| Odd numbers | for(i=1; i<=10; i+=2) | 1 3 5 7 9 |
| Powers of 2 | for(i=1; i<=256; i*=2) | 1 2 4 8... 256 |
For Loop vs While Loop
| Aspect | For Loop | While Loop |
|---|---|---|
| Structure | All in one line | Spread across code |
| Best for | Known iterations | Unknown iterations |
| Variable scope | Can be local to loop (C99) | Declared outside |
| Readability | Compact and clear | More flexible |
| Use case | Arrays, counting, patterns | Sentinel, flag, events |
Best Practices
- Use meaningful variable names for complex loops (not just
i) - Declare loop variables in the for statement (C99:
for(int i=0;...)) - Don't modify the loop variable inside the body — it creates confusion
- Use
size_tfor array indexing to avoid signed/unsigned warnings - Keep loop bodies short — extract complex logic into functions
Interview Questions
Q1: What happens if all three parts of a for loop are empty: for(;;)?
It creates an infinite loop. The missing condition is treated as always true. You must use break or return inside the body to exit. This is equivalent to while(1).
Q2: Can we declare multiple variables of different types in a for loop initialization?
No. The initialization can declare multiple variables only of the same type (e.g., for(int i=0, j=10; ...)). You cannot mix types like for(int i=0, float f=1.0; ...) — that's a syntax error.
Q3: What is the scope of a variable declared in the for loop header?
In C99 and later, a variable declared in the for initialization (like for(int i=0;...)) has its scope limited to the for statement (including the body). It doesn't exist outside the loop.
Q4: How many times does the update expression execute compared to the condition?
The condition executes one more time than the update. If the loop runs N times, the condition is checked N+1 times (the last check fails and exits), while the update runs exactly N times.
Q5: In a nested loop with outer loop running M times and inner loop N times, how many times does the inner body execute?
The inner loop body executes M × N times total. For each of the M iterations of the outer loop, the inner loop completes all N iterations.
Summary
The for loop is C's most versatile and commonly used loop structure. Its compact syntax — putting initialization, condition, and update in one line — makes code clear and maintainable. Use it for counting, array traversal, patterns, and any situation where the number of iterations is known. Nested for loops enable powerful patterns like matrices and pyramids. Remember that all parts are optional, multiple variables can share the initialization, and C99 allows loop-scoped variable declarations.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for For Loop in C Programming.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, control, statements, for, loop, for loop in c programming
Related C Programming Topics