FOR loop in C programming language – Iteration Statements

Iteration is the process where a set of instructions or statements is executed repeatedly for a specified number of time or until a condition is met. These statements also alter the control flow of the program and thus can also be classified as control statements in C Programming Language.

Iteration statements are most commonly know as loops. Also the repetition process in C is done by using loop control instruction. There are three types of looping statements:

  • For Loop
  • While Loop
  • Do-while loop

A loop basically consists of three parts: initialization, test expression, increment/decrement or update value. For the different type of loops, these expressions might be present at different stages of the loop. Have a look at this flow chart to better understand the working of loops(the update expression might or might not be present in the body of the loop in case break statement is used):

loop-flow-chart

In this tutorial, we will be learning about the most common and popular loop statement: The For Loop.


For Loop

For loop is the most commonly used looping technique. The reason why it is so popular is because it has all the three parts of the loop: initialization, test expression, update expression in the same line. The syntax of a for loop is:

for(initialization; test expression; update expression)
{
  //body of loop
}

Have a look at this simple for loop program for a better understanding:

#include <stdio.h>

int main()
{
    int i;
    for(i=1; i<=5; i++)
        printf("CodinGeek\n");
    return 0;
}

Output:-
CodinGeek
CodinGeek
CodinGeek
CodinGeek
CodinGeek

Here we have given the initial value of i as 1(initialization). The test condition is i<=5 and the update expression is i++.
Let’s understand how this works. In the beginning, the control goes to the initial condition and assigns the value 1 to i. Now it checks whether 1 <=5 which is true. Since the condition is true the control flows to the body of the loop and prints CodinGeek. As the body ends the control flows back to for again. Now, it updates the value of i(i=2) and checks the condition again whether 2<=5 which stand true again. So the control again flows to the body of the loop and prints CodinGeek again(Note that the control does not go back to the initialization expression in the next iteration).
Similarly, the process continues for values of i =34 and 5. When i becomes equal to 6, the condition i<=5 becomes false and the loop terminates and the program ends.


Different Types of for loop

For loops can also be of different types, let’s have a look at some of them:

Infinite for loop

An infinite for loop is the one which goes on repeatedly for infinite times. This can happen in two cases:

  • When the loop has no expressions.
for(;;)
{
  printf("CodinGeek");
}

The above code runs infinite times because there is no test expression to terminate the loop. Hence, CodinGeek gets printed infinite times.

  • When the test condition never becomes false.
for(i=1;i<=5;)
{
  printf("CodinGeek");
}

In the above case, the statement CodinGeek gets printed infinite times because the test condition never becomes false. The value of i remains the same because it is never updated and thus the value of i remains smaller than 5 for each iteration.


Empty For Loop

An empty for loop is the one which has got no body. In simple words, it contains no statements or expressions in its body. It can be used for time-consuming purposes. Each iteration takes its own time for compilation and execution. Usually, it is too small to be of any significant importance. Here’s the syntax of an empty for loop:

for(i=1;i<=5;i++)
{}

Nested For Loop

In the previous tutorial of if-else control statements we saw what nested if is. Similarly, nested for loop refers to the process of having one loop inside another loop. We can have multiple loops inside one another. The body of one ‘for’ loop contains the other and so on. The syntax of a nested for loop is as follows(using two for loops):

 
for(initialization; test; update) 
{
  for(initialization;test;update)//using another variable
  {
    //body of the inner loop
  }
  //body of outer loop(might or might not be present)
} 

The following program will illustrate the use of nested loops:

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

Output:-
1
12
123
1234
12345

This program prints a triangular pattern. Now, the outer loop starts with i=1 and checks the condition whether i<= 5 is true or not which is true so the control goes on the body which contains another loop.
This loop starts with the initial value as j=1 and checks if j<=i which is true and hence prints the value of j. In the next iteration, the condition of the inner loop becomes false and the control exits the inner loop and changes the line. Now it goes for the next iteration of the outer loop and the process goes on..unless the condition becomes false and the loop terminates and the program ends.

Knowledge is most useful when liberated and shared. Share this to motivate us to keep writing such online tutorials for free and do comment if anything is missing or wrong or you need any kind of help.

Do not forget to share and Subscribe.

Keep Learning… Happy Learning.. 🙂