Mastering the For Loop in C: Syntax, Logic and Examples

Table of Contents

What Is a for Loop in C?

A for loop in C is a control structure that allows you to execute a block of code a fixed number of times. Instead of writing the same code repeatedly you can tell the computer:

“Do this task n times.”

Syntax of for Loop in C

 
for (initialization; condition; update) {
    // code to be executed
}	

Breakdown of Each Part            

  • Initialization: It sets the starting value of the loop variable and executes only once at the very beginning of the loop.

  • Condition: It evaluates a boolean expression before every iteration to determine whether the loop should continue running or terminate.

  • Update: It modifies the loop variable (increment or decrement) after the loop body executes to prepare for the next condition check.

Simple Example: Write a C program to print Numbers from 1 to 5

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

// 1 2 3 4 5 
				
			
Value of icheck i <= 5OutputUpdates
1True1i is incremented to 2
2True2i is incremented to 3
3True3i is incremented to 4
4True4i is incremented to 5
5True5i is incremented to 6
6False-terminates the loop
The image displays the flowchart of the for loop in C. It shows every iteration of the for loop.
C_for_loop_flowchart

Why Use a for Loop?

The for loop is perfect when:

  • You know how many times the loop should run
  • You want clean and readable code
  • You are working with arrays, strings or patterns

 

Write a  C program to print the sum of n numbers using the for loop

				
					#include <stdio.h>

int main() {
    int sum = 0;
    for (int i = 1; i <= 10; i++) {
        sum += i;
    }
    printf("Sum = %d", sum);
    return 0;
}

				
			
value of icheck (i <= 10)(sum + i)SumIncrement
0Enter Loop
1True0 + 11i is incremented to 2
2True1 + 23i is incremented to 3
3True3 + 36i is incremented to 4
4True6 + 410i is incremented to 5
5True10 + 515i is incremented to 6
6True15 + 621i is incremented to 7
7True21 + 728i is incremented to 8
8True28 + 836i is incremented to 9
9True36 + 945i is incremented to 10
10True45 + 1055i is incremented to 11
11False55Loop ends

Write a C program to print the Fibonacci Series by using for loop

				
					#include <stdio.h>
int main() {
    int n= 10, a = 0, b = 1, c;
    printf("Fibonacci Series: ");
    for (int i = 1; i <= n; i++) {
        printf("%d ", a);
        c = a + b;
        a = b;
        b = c;
    }
    return 0;
}

// Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 
				
			
ia (Printed)bc = a + bNew aNew b
101111
211212
312323
423535
535858

Nested for Loop (Loop Inside a Loop)

 

Placing one loop inside another is known as a nested loop. For every single iteration of the outer loop the inner loop runs through its entire cycle from beginning to end.

 

				
					#include <stdio.h>

int main() {
    int i, j;

    for (i = 1; i <= 4; i++) {
        for (j = 1; j <= i; j++) {
            printf("%d ", j);
        }
        printf("\n");
    }

    return 0;
}

// 1 
// 1 2 
// 1 2 3 
// 1 2 3 4 

				
			
i (Row)j (Col)Check ( j <= i )OutputVisual Output (Current Line)
11True11
12False\n(end of line)
21True11
22True21 2
23False\n(end of line)
31True11
32True21 2
33True31 2 3
34False\n(end of line)
41True11
42True21 2
43True31 2 3
44True41 2 3 4
45False\n(end of line)
5Loop ends

Applications

1) Repeated Execution of Statements: This looping structure is used to execute a set of instructions multiple times when the number of repetitions is already known. It helps reduce code length and improves program efficiency.

2) Array and String Traversal: It is widely used to access each element of an array or each character of a string in sequence. This makes operations like searching, sorting, and updating data easier and faster.

3) Mathematical and Logical Calculations: This structure is useful in performing calculations such as sum of numbers, factorial, prime number checking and Fibonacci series by repeating arithmetic operations.

4) Pattern Generation: It is used to generate different patterns such as stars, numbers and pyramids. Pattern programs help improve logical thinking and understanding of nested repetition.

5) Data Processing and Analysis: In real-world applications, it processes large amounts of data such as student marks, sales records, and survey information to calculate totals, averages and other statistical values.

6) Game and Graphics Development: It plays an important role in controlling animations, character movement and drawing grids or frames by updating object positions repeatedly.

7) Embedded Systems and Hardware Control: In embedded systems, it is used to control hardware components such as LEDs, motors and sensors by repeating operations at fixed intervals.

Frequently Asked Questions ( FAQs)

1) What is the syntax of C for loop?

The syntax of a for loop in C is:

for (initialization; condition; update) {

    // statements

}

It allows a block of code to run repeatedly until the condition becomes false.

 

2) What is a nested for loop in C?

A nested for loop is a for loop written inside another for loop.
It is commonly used for patterns, matrices, and table generation.

 

3) Can a for loop run without a condition?

Yes. If the condition is omitted, the for loop becomes an infinite loop and runs continuously unless terminated using break.