Table of Contents
show
The execution of for statement is executed as,
- Initialization section is executed only once at the start of the loop
- The expression present in the condition section is evaluated
- If it evaluates to true, the body of the loop is executed
- If it is evaluated to false, the loop terminates and the program control is transferred to the statement present next to the for statement
- After the execution of the body of the loop, the manipulation expression is evaluated
Syntax
for(expression1;expression2;expression3;)
statement
Flow chart
Example
Sum of first n natural numbers
#include<stdio.h>
int main()
{
int n,lc,sum=0;
printf("Enter the value \t");
scanf("%d",&n);
for(lc=1;lc<=n;lc++)
{
sum = sum + lc;
}
printf("Sum is %d",sum);
return 0;
}
Output
Enter the value 5
Sum is 15
Views: 0