01. The For Loop & C99 Block Declarations
🟢 Difficulty: Beginner | Entry-Controlled Counters, Block Scope & Iteration
📖 Definition
A for loop in C is an entry-controlled iterative control structure used to execute a block of code a specific number of times. It bundles initialization, condition testing, and counter modification into a single header statement.
🎯 Why It Is Used
- Deterministic Iteration: Ideal when the total number of iterations is known beforehand (e.g. iterating over array indices from
0toN-1). - C99 Block-Scoped Counter Scope: Declaring variables inside the loop header (
for (int i = 0; ...)) keeps the counter scoped strictly to the loop body, preventing variable leakage.
⚙️ How It Works: Execution Sequence
- Initialization: Executes once before entering the loop.
- Condition Test: Evaluates the condition expression. If non-zero (true), execution enters the loop body; if zero (false), the loop terminates immediately.
- Body Execution: Executes the code block inside
{}. - Modifier Step: Executes the increment/decrement expression (e.g.
i++), then jumps back to Step 2.
📝 Syntax
for (initialization; condition; modifier) {
// Loop body statements
}
🔀 Infinite Loops & Multi-Variable Headers
- Infinite Loop Pattern: Omitting all three parts
for (;;) { ... }creates an intentional infinite loop. - Multi-Variable Initialization: Using comma operators:
for (int i = 0, j = 10; i < j; i++, j--).
🌱 Beginner Example
#include <stdio.h>
int main(void) {
// C99 block-scoped counter 'i'
for (int i = 1; i <= 5; i++) {
printf("Iteration %d\n", i);
}
return 0;
}
🌼 Intermediate Example: Array Traversal & Sum Calculation
#include <stdio.h>
int main(void) {
int numbers[] = {12, 24, 36, 48, 60};
size_t len = sizeof(numbers) / sizeof(numbers[0]);
int sum = 0;
for (size_t i = 0; i < len; i++) {
sum += numbers[i];
printf("Index %zu: Value = %d | Running Sum = %d\n", i, numbers[i], sum);
}
return 0;
}
🌲 Advanced Example: Multi-Variable Convergence Loop
#include <stdio.h>
int main(void) {
printf("=== Dual Pointer Converging Loop ===\n");
for (int left = 0, right = 10; left <= right; left++, right--) {
printf("Left Index: %d <-> Right Index: %d\n", left, right);
}
return 0;
}
🔍 Line-by-Line Explanation
for (size_t i = 0; i < len; i++): Standard idiom for array traversal starting at index0up tolen - 1.sizeof(numbers) / sizeof(numbers[0]): Computes element count for fixed array at compile time.for (int left = 0, right = 10; ...): Uses comma operators to maintain two converging counter variables simultaneously.
💻 Expected Terminal Output
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
Index 0: Value = 12 | Running Sum = 12
Index 1: Value = 24 | Running Sum = 36
Index 2: Value = 36 | Running Sum = 72
Index 3: Value = 48 | Running Sum = 120
Index 4: Value = 60 | Running Sum = 180
=== Dual Pointer Converging Loop ===
Left Index: 0 <-> Right Index: 10
Left Index: 1 <-> Right Index: 9
Left Index: 2 <-> Right Index: 8
Left Index: 3 <-> Right Index: 7
Left Index: 4 <-> Right Index: 6
Left Index: 5 <-> Right Index: 5
⚠️ Common Mistakes & Pitfalls
- Off-by-One Array Boundary Errors: Writing
i <= leninstead ofi < lencauses Out-of-Bounds memory reads/writes. - Semicolon After Loop Header: Writing
for (int i=0; i<5; i++);executes an empty loop body 5 times, then runs the block below once! - Modifying Counter Inside Loop Body: Altering
iinside both the loop body and header causes unexpected skips or infinite loops.
📌 Important Rules & Limitations
- Variables declared inside C99
for (int i = 0; ...)headers exist only inside the loop block. - Always use
size_tfor loop counters indexing memory arrays.
🌐 Multilingual Explanation: English
A for loop bundles initialization, condition testing, and counter steps. Use C99 block-scoped declarations (for (int i = 0; ...)) to keep loop counters localized.
🌐 Multilingual Explanation: Hindi (Roman Script)
for loop fixed number of iterations ke liye use hota hai. C99 mein hum variable ko seedhe loop header (for (int i = 0; ...)) mein declare kar sakte hain.
🌐 Multilingual Explanation: Marathi (Roman Script)
for loop thavik veles code repeat karnyasathi vapartat. Array indices traverse karnyasathi 0 te N-1 pargent for loop best aste.
❓ Practice Questions
- What is the difference in variable scope between
int i; for(i=0;...)andfor(int i=0;...)in C99? - Why does
for (int i = 0; i <= len; i++)cause an off-by-one buffer overflow error on an array of lengthlen? - How does
for (;;)create an infinite loop?
🚀 Practical Coding Challenge
Write a C program named multiplication_table.c that prompts for an integer N and uses a for loop to display its multiplication table from N x 1 to N x 10.