For Loop in C Language: Syntax, Execution & Examples (Unit 2)



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

BKNMU Junagadh NEP 2020 Unit 2 For Loop Iteration Control Nested Loops
In simple words: Welcome to Unit 2! The 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.
📖 Anatomy of a For Loop

The for loop statement consists of three essential control expressions separated by semicolons:

for (i = 1; ...)

1. Initialization

Executes strictly ONCE before the loop begins. Sets the initial value of the loop counter variable.

... i <= 10; ...

2. Condition Check

Evaluated before every iteration. If True (non-zero), the body executes; if False (zero), the loop terminates.

... i++)

3. Increment / Decrement

Executes AFTER each loop body iteration to update the loop counter variable before re-checking the condition.

for (i...) { for (j...) }

4. Nested For Loop

Placing one for loop inside another to handle multi-dimensional operations like matrices and star patterns.

1️⃣ Master Table: For Loop Expressions

Here is a detailed structural summary of the three components in a C language for loop header:

Loop ComponentExecution OrderPurpose & Functional Behavior
InitializationStep 1 (First step only)Initializes loop variables. Can initialize multiple variables separated by commas (e.g., i=0, j=10).
Test ConditionStep 2 (Before loop body)Determines whether to continue or exit loop. If omitted, C treats it as implicitly True (creating an infinite loop).
Counter UpdateStep 4 (After loop body)Increments (i++) or decrements (i--) the counter. Runs right after executing the loop body.
ℹ️ Why Entry-Controlled? A 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!
💻 Code Examples & Practical Applications

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-by-Step Execution Sequence
1

Step 1: Initialize Counter

The initialization expression runs once to set initial variable values.

2

Step 2: Check Condition

The condition is tested. If True, go to Step 3. If False, jump straight out of the loop.

3

Step 3: Execute Body & Update

The inner loop code runs, and then the increment/decrement statement updates the counter. Return to Step 2.

💡 Infinite Loop Trick: Writing for (;;) creates an intentional **infinite loop** because all three expressions are optional in C. It will run endlessly until terminated with a break statement.
⚠️ Common Semicolon Error: Putting a semicolon directly after the loop header like for (i=0; i<10; i++); terminates the loop header immediately, causing the subsequent block to run only ONCE outside the loop context!
📝 Quick Revision — Key Exam Points
  • 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.

Post a Comment

Thanks for comment.

Previous Post Next Post