for loop in C Language Tutorial

This “for-loop C language Tutorial” page explains clearly about for loop including syntax, flow chart and execution flow using example codes.


for-loop :

  • Used to execute a block repeatedly.
  • Terminates if the given condition is true.
  • Execute a block by placing initialization, condition and modify statements in one instruction.
  • Initialization statement executes only once in the application.

 

 

Print 1 to 10 integers :
void main()
{
int i;
for(i=1 ; i<=10 ; i++)
{
printf(“i : %d \n”, i);
}
}


Print Even numbers between 1-10 :
void main()
{
int i;
for(i=1 ; i<=10 ; i++)
{
if(i%2==0)
{
printf(“i : %d \n”, i);
}
}
}


Sum of first n numbers:
void main()
{
int n,i,sum=0;
printf(“Enter the limit : “);
scanf(“%d”,&n);
for(i=1 ; i<=n ; i++)
{
sum=sum+i; // sum+=i;
}
printf(“sum of first %d numbers is : %d”,n, sum);
}

Share this