For loop in C language

for loop is a repetition control structure. It is used to iterate the statements or a part of the program several times.

In this tutorial, you will learn to create for loop in C programming with the help of examples.

It is frequently used to traverse the data structures like the array and linked list.

Syntax

for (initializationStatement; testExpression; updateStatement/increment)
{
    // statements inside the body of loop
}
  • The initialization statement is executed only once. The initialization step is executed first, and only once. This step allows you to declare and initialize any loop control variables.
  • Next, the expression is evaluated. If the test expression is evaluated to false, the for loop is terminated.
  • However, if the test expression is evaluated to true, statements inside the body of the for loop are executed. After the body of the ‘for’ loop executes, the flow of control jumps back up to the increment statement. 
  • Again the test expression is evaluated. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition).
  • This process goes on until the test expression is false. When the test expression is false, the loop terminates.

Flowchart of for loop in C

Flowchart of for loop in C

Example

// Print numbers from 1 to 10
#include <stdio.h>

int main() {
  int i;

  for (i = 1; i < 11; ++i)
  {
    printf("%d ", i);
  }
  return 0;
}
For loop in C language Example – Output