TopBottom

Followers



Click on more
SUBSCRIBE

Enter your email address:

Delivered by FeedBurner



VIDEO

Announcement: wanna exchange links? contact me at ravikrak@yahoo.com

FOR LOOP in C lang

Posted by Ravi Kumar at Thursday, September 1, 2011
Share this post:
Ma.gnolia DiggIt! Del.icio.us Yahoo Furl Technorati Reddit

The for loop is anotheer entry controlled loop that
provides a more concise loop control structure.The general
form of the loop is:
for(initialization; test-condition ;increment)
{
body of the loop
}

The execution of the for statement is as follows:

1.Initalization of the control variables is done first,using
assignments statements such as i=1 and count=0.The variable
i and count are known as loop-control.

2. The value of the control variable is tested using the
test-condition.The
test-condition is a relational
expression such as (i<10)that determines when the loop will
exit.If the condition is true,the body of loop is executed
otherwise the loop is terminated and the execution continues
with the statement that immediately follows the loop.

3. When the body of the loop is executed,the control is
transferred back to the for statement after evaluating the
last statement in the loop.Now,the control variable increme
nted using an assignment statement such as i=i+1 and the new
value of the control variable is again tested to see whether
it satisfies the loop condition.If the condition is satisfied,
the body of the loop is again executed.The process continues
until the value of the control variables fails to satisfy the
test condition.

Example:
Program to find sum of 'n' numbers
#include "stdio.h"
void main()
{ int i,n,sum=0;
printf("Enter the value of n \n");
scanf("%d", &n);
for(i=0;i < n;i++)
sum+=i;
printf("Sum of %d numbers=%d",n,sum);
}

output: Enter the value of n
10
Sum of 10 numbers=55

Here the 'i' is initalized to zero,which is again
incremented until it is less than n,which is tested within
the loop.



Share |

Labels:

0 comments:

Post a Comment