for loop is a counter controlled loop,perhaps the most commonly used looping statement in C programming. It is used to repeatedly execute either a single or a group of statement for a finite number of times.
Syntax-:
for(Initialization;Testing;Increment/Decrement)
- Initialization-: This expression is used to initialize same parameter [called an index],that control the looping action.
- Testing Expression-: It represent a condition that must be TRUE for the loop to continue its execution.
- Increment/Decrement Expression-: It is a unary expression or an assignment expression.It increment or decrement the index so that the loop continue.
#include<stdio.h>
#include<conio.h>
main()
{ int i=0;
for(i=1;i<=5;i++)
printf("\n %d",i);
getch();
}
Output-: 1
2
3
4
5
Example-:write a program to find sum of series 1+2+3+----
#include<stdio.h>
#include<conio.h>
main()
{int i=0,term=0,s=0;
printf("Enter the term");
scanf("%d",&term);
for(i=1;i<=term;i++)
s=s+i;
printf("\n sum=%d",s);
getch();
}
Output-: Enter the term 3
sum=7
subscribe the blog
ReplyDelete