for Loop in C

The for loop in C language is also used to iterate the statement or a part of the program several times, like while and do-while loop.

Unlike do while loop, the condition or expression in for loop is given before the statement, so it may execute the statement 0 or more times.

Syntax for Loop in C

for(initialization;condition;incr/decr){ 
//code to be executed

}         
			

For loop contains tree parts


  • Initialization
  • Condition
  • Iteration
  1. When we are working with for loop always execution process will start from initialization block.
  2. After initialization block control will pass to condition block, if condition is evaluated as true then control will pass to statement block.
  3. After execution of the statement block control will pass to iteration block, from iteration it will pass back to the condition.
  4. Always repetitions will happen beginning condition, statement block and iteration only.
  5. Initialization block will be executed only once when we are entering into the loop first time.
  6. When we are working with for loop everything is optional but mandatory to place 2 semicolons (; ;)

Example of for loop in C


#include<stdio.h>
#include<conio.h>
void main(){ 
int i=0; 
clrscr(); 



for(i=1;i<=10;i++){ 
printf("%d \n",i); 
} 
getch(); 

} 				

Output:

1
2
3
4
5
6
7
8
9
10