for loop in C Programming

Looping is a process of repeating a certain group of statements until a specified condition is satisfied. There are three types of loop in C. They are:

For loop is an entry controlled loop i.e. the condition is checked before entering into the loop. So if the condition is false for the first time, the statements inside while loop may not be executed at all. In order to exit from a for loop, either the condition should be false or a break statement should be encountered. For loop is suitable to use when we have to run a loop for a fixed number of times.

Syntax of for loop

for (initialization; condition; increment/decrement)
    ... ... ...

Components of for loop

For loop consists of three components

1. Initialization​

In this part, the variable required for the loop is initialized. It is executed only once at the first iteration. This part is optional and may be absent.

For example:

for(i=0; condition; increment/decrement)

for( ; condition; increment/decrement)  // initialization is absent

Here, in the first example, i = 0 is the initialization variable while in the second example, no initialization is done.

2. Condition

Here, condition for executing the loop is checked. The condition is checked after the execution of increment/decrement statement. If the condition is true then loop is executed, otherwise it is terminated. If this part is left blank, it is considered true in C causing the loop to run infinite times.

For example:

for(i=0; i<=10; increment/decrement)

for(i=0 ; i<strlen(name); increment/decrement)

In these examplesi<=10 and i <strlen(name) are conditions.

3. Increment/Decrement

This part increments or decrements the value of a variable that is being checked. This part is executed at the end of each iteration before executing the conditional part. It is also optional part of for loop.

For example:

for(i=0; i<=10; i++)  // increment

for(i=10 ; i>0; i--)  // decrement

In these examples, i++ and  i-- are increment, and decrement components respectively.

Flowchart of for loop

flowchart of for loop in C programming

Infinite for loop

There may be a condition in a for loop which is always true. In such case, the loop will run infinite times. For example,

for (i=0; i>0; i++)
    printf("This is infinite loop");

In the example, program execution checks the condition i > 0 which always holds true. It makes it run infinite times printing the underlying statement This is infinite loop.

Infinite loop with no conditions

Likewise, when the condition part is left blank, the C compiler will also treat it as true and the loop will run infinite times.

for (i=0; ; i++)
    printf("This is infinite loop");
for (;;)
    printf("This is infinite loop");

Break statements in Infinite loop

We can use break statement to exit from an infinite loop. For example,

for (;;)
    printf("This loop will run only once");

Example of for loop

Program1: C program to find the sum of first n natural numbers.

int main()
    int i,n,s=0;
    printf("Enter value of n:");
    printf("Sum = %d",s);
    return 0;

This program prints the sum of first n natural numbers. A number is asked from user and stored in variable n. For loop is used to add numbers from 1 to n and store the sum in s. Variable i is used for looping and it is incremented on each iteration. The condition is checked and until i is less than or equal to n, the loop runs. Finally the value of sum is printed after terminating the loop.


Enter value of n:15
Sum = 120