For loop in C programming

In real life we come across situations when we need to perform a set of task repeatedly till some condition is met. Such as – sending email to all employees, deleting all files, printing 1000 pages of a document. All of these tasks are performed in loop. To do such task C supports looping control statements.

For loop is an entry controlled looping statement. It is used to repeat set of statements until some condition is met.

Looping statements whose condition is checked prior to the execution of its body is called as Entry controlled loop.

Syntax of for loop

for(variable-initialization ; condition ; variable-update)
{
    // Body of for loop
}

Parts of for loop

Any repetition contain two important part – What to repeat and number of repetition? variable-initialization, condition and variable-update define number of repetition and body of loop define what to repeat.

  • Variable-initialization contain loop counter variable initialization statements. It define starting point of the repetition (where to start loop).
  • Condition contain boolean expressions and works like if…else. If boolean expression is true, then execute body of loop otherwise terminate the loop.
  • Body of loop specify what to repeat. It contain set of statements to repeat.
  • Variable-update contains loop counter update (increment/decrement) statements.

Important note: All four parts of a for loop is optional. Hence you can write a for loop without initialization, condition, update or body. However, you must follow the syntax and specify semicolons.

How for loop works?

  1. Initially variable-initialization block receive program control. It is non-repeatable part and executed only once throughout the execution of for loop. After initialization program control is transferred to loop condition.
  2. The loop condition block evaluates all boolean expression and determines loop should continue or not. If loop conditions are met, then it transfers program control to body of loop otherwise terminate the loop. In C we specify a boolean expression using relational and logical operator.
  3. Body of loop execute a set of statements. After executing all statements it transfer program control to variable-update block.
  4. Variable-update block updates loop counter variable and transfer program control again back to condition block of loop.

Step 2 to 4 is repeated until condition is met.

Flowchart of for loop

For loop flowchart

Example program to demonstrate for loop

Let us write a C program that print natural numbers from 1 to 10.

/**
 * C program to print natural numbers from 1 to 10.
 */

#include <stdio.h>

int main()
{
    /* Declare loop counter variable */
    int count;

    /* Run a loop from 1 to 10 */
    for(count=1; count<=10; count++)
    {
        /* Print current value of count */
        printf("%d ", count);
    }

    return 0;
}

Output

1 2 3 4 5 6 7 8 9 10

To get a better hands on for loop practice exercises based on loop.