Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. Input/Output statements
  5. Looping statements
  6. for loop

for loop

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

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments