for loop in c

In c, for loop is an entry control loop, which repeat statements until test condition become false. It is one of the easy to understand loop among other loops.

Syntax

for(initialization; condition; iteration)
{
  // statements;
}

For loop has three parts

initializationThe initial value from where the for loop starts.
conditionIt specifies the number of times loop will repeat itself.
iterationIt either increases or decreases the loop cycle to continue.

Example

#include<stdio.h>

void main(){
  for(int i = 0; i < 10; i++)
  {
    printf("%d ", i);
  }
}
Output:
1 2 3 4 5 6 7 8 9

Explanation

  1. i=0; means initial or starting value of i is 0 and loop starts from value 0.
  2. i < 10; It is test condition. Until test condition is true means value of i is less than 9, loop executes statements, Once value of i becomes 9, test condition becomes false and loop stops execution.
  3. i++ means i = i + 1, iteration increases the value of i by 1 each time, until test condition becomes false.