BCA Sem 1 | Unit 2 | Computer Concepts & C Programming
For Loop in C Language — BKNMU Junagadh
Syntax, Entry-Controlled Looping Mechanics, Nested Loops, and Infinite Loop Pitfalls
for loop is an entry-controlled iteration statement in C programming. It allows a specific block of code to execute repeatedly for a fixed number of times by combining initialization, condition checking, and counter update into a single compact header statement.
The for loop statement consists of three essential control expressions separated by semicolons:
1. Initialization
Executes strictly ONCE before the loop begins. Sets the initial value of the loop counter variable.
2. Condition Check
Evaluated before every iteration. If True (non-zero), the body executes; if False (zero), the loop terminates.
3. Increment / Decrement
Executes AFTER each loop body iteration to update the loop counter variable before re-checking the condition.
4. Nested For Loop
Placing one for loop inside another to handle multi-dimensional operations like matrices and star patterns.
Here is a detailed structural summary of the three components in a C language for loop header:
| Loop Component | Execution Order | Purpose & Functional Behavior |
|---|---|---|
Initialization | Step 1 (First step only) | Initializes loop variables. Can initialize multiple variables separated by commas (e.g., i=0, j=10). |
Test Condition | Step 2 (Before loop body) | Determines whether to continue or exit loop. If omitted, C treats it as implicitly True (creating an infinite loop). |
Counter Update | Step 4 (After loop body) | Increments (i++) or decrements (i--) the counter. Runs right after executing the loop body. |
for loop is called an entry-controlled loop because the test condition is evaluated at the beginning of each iteration. If the initial condition is False, the loop body will not execute even once!
1. Basic For Loop (Printing Numbers 1 to 5):
#include <stdio.h>
int main() {
int i;
// Standard for loop execution
for (i = 1; i <= 5; i++) {
printf("Iteration Count: %d\n", i);
}
return 0;
}
2. Sum of First N Natural Numbers:
#include <stdio.h>
int main() {
int n = 10, sum = 0, i;
for (i = 1; i <= n; i++) {
sum += i;
}
printf("Sum of 1 to %d = %d\n", n, sum);
return 0;
}
3. Nested For Loop Example (Printing Right-Angle Star Pattern):
#include <stdio.h>
int main() {
int i, j;
// Outer loop controls rows, inner loop controls columns
for (i = 1; i <= 4; i++) {
for (j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Step 1: Initialize Counter
The initialization expression runs once to set initial variable values.
Step 2: Check Condition
The condition is tested. If True, go to Step 3. If False, jump straight out of the loop.
Step 3: Execute Body & Update
The inner loop code runs, and then the increment/decrement statement updates the counter. Return to Step 2.
for (;;) creates an intentional **infinite loop** because all three expressions are optional in C. It will run endlessly until terminated with a break statement.
for (i=0; i<10; i++); terminates the loop header immediately, causing the subsequent block to run only ONCE outside the loop context!
- Loop Type: Entry-Controlled Iteration Loop.
- Header Syntax:
for (initialization; condition; update) - Delimiter requirement: Expressions inside parentheses must be separated by two semicolons (
;). - Minimum Executions: 0 times (if initial condition is False).
- Best Used For: Scenarios where the total number of iterations is known in advance.