For Loop in C

The for loop in C programming is an entry-controlled looping statement that enables a block of code to be executed repeatedly for a fixed, predetermined number of iterations. It combines the initialization, condition evaluation, and loop counter increment/decrement into a single, compact statement, making it the most popular and readable loop structure in C.

For Loop Flowchart

Syntax of For Loop


for (initialization; condition; increment/decrement)
{
    /* Statements to execute repeatedly */
}
							

Step-by-Step Execution Flow

 Step 1 (Initialization): Executed only once when the loop begins. Used to declare or initialize the loop control counter (e.g., i = 1).

 Step 2 (Condition Check): Evaluates a boolean expression. If the condition is true (non-zero), execution proceeds into the loop body. If false (zero), the loop terminates immediately.

 Step 3 (Loop Body): The block of code enclosed within curly braces executes.

 Step 4 (Increment / Decrement): Modifies the loop counter (e.g., i++ or i--). Control then jumps back to Step 2.

Standard Working Example: Sum of First N Numbers


#include <stdio.h>

int main()
{
    int n, i, sum = 0;

    printf("Enter a positive integer N: ");
    scanf("%d", &n);

    for(i = 1; i <= n; i++)
    {
        sum += i;  /* sum = sum + i */
    }

    printf("Sum of first %d natural numbers = %d\n", n, sum);

    return 0;
}
							

Advanced Variations of For Loop in C

The flexibility of C allows several powerful variations of the standard for loop:

1. Infinite For Loop

Omitting all three expressions creates an infinite loop that continues until an explicit break or exit() is encountered:


for ( ; ; ) {
    printf("This runs forever until interrupted.\n");
    break; /* Exits infinite loop */
}
							

2. Multiple Loop Variables (Comma Operator)

You can initialize and update multiple counters simultaneously using the comma (,) operator:


int i, j;
for(i = 1, j = 10; i <= 5; i++, j--) {
    printf("i = %d, j = %d\n", i, j);
}
							

3. Omitting Expressions

Any or all expressions can be placed outside the loop header:


int i = 0;
for( ; i < 5; ) {
    printf("%d ", i);
    i++; /* Increment inside loop body */
}
							

Nested For Loops: Star Pattern Printing (BCA Exam Classic)

A loop inside another loop is called a nested loop. The outer loop controls rows, while the inner loop controls columns:


#include <stdio.h>

int main() {
    int rows = 5, i, j;

    for(i = 1; i <= rows; i++) {
        for(j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}
							

Output:


* 
* * 
* * * 
* * * * 
* * * * * 
							

Practice Session for Beginner Students

Enhance your problem-solving skills with these dry-run puzzles, university MCQs, and hands-on coding assignments.

Part 1: Spot the Bug & Predict the Output

Problem 1: What will be printed by this code snippet?


#include <stdio.h>
int main() {
    int i;
    for(i = 0; i < 5; i++);
    {
        printf("%d", i);
    }
    return 0;
}
							

Answer & Explanation: Output is 5. Notice the semicolon ; immediately following the for header. The semicolon represents an empty statement (null statement). The loop runs 5 times doing nothing, and only when i becomes 5 does it exit and execute the printf block once.

Part 2: Multiple Choice Questions (BCA Exam Special)

Q1. What type of loop is the for loop categorized as?
A) Exit-controlled loop
B) Entry-controlled loop
C) Post-test loop
D) Unconditional loop
Correct Answer: B) Entry-controlled loop (Because condition is tested before entering the loop body).

Q2. What happens if the condition expression is left blank in for(i=0; ; i++)?
A) Compilation error
B) The loop executes 0 times
C) The condition is treated as perpetually true (infinite loop)
D) The loop executes once
Correct Answer: C) The condition is treated as perpetually true.

Q3. Which statement is used to skip the rest of the current iteration and jump to the next increment step?
A) break
B) continue
C) return
D) goto
Correct Answer: B) continue.

Part 3: Hands-on Lab Assignments for Beginners

Assignment 1: Write a C program to calculate and print the factorial of a user-given number using a for loop.
Logic Hint: long long fact = 1; for(i = 1; i <= n; i++) fact *= i;.

Assignment 2: Write a program to print the Fibonacci series up to N terms (0, 1, 1, 2, 3, 5, 8, ...).
Logic Hint: Keep track of t1 = 0, t2 = 1, and inside the loop compute nextTerm = t1 + t2; t1 = t2; t2 = nextTerm;.

Part 4: Frequently Asked University Viva Questions

1. When should you use a for loop instead of a while loop?
Answer: Use a for loop when the number of iterations is known in advance (fixed count). Use a while loop when iteration depends on a dynamic condition or unknown user input.

2. Can a for loop have a negative step counter?
Answer: Yes. For example: for(i = 10; i >= 1; i--) counts down from 10 to 1.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!