Pages

Tuesday 9 August 2011

For Loop




A for loop allows execution of a statement (or a block of statements) repeatedly a number of times. When the number of times a statement is to be executed is known in advance, for loop is particularly useful. The syntax of for loop in C language is as follows:
for(<initialization>; <condition>; <modification>)
Statement-1;
Note that a for loop in C language contains three expressions. <Initialization> part, <condition> part and <modification> part.
The initialization part is usually an assignment statement that is used to set the loop control variable. The condition is a relational expression that determines when the loop will exit. The modification part defines how loop control variable will change each time the loop is repeated. These three sections are separated by a semicolon. The for loop will execute as long as the condition evaluates to true. Once the condition

becomes false, the loop is exited and the program execution will resume on the statement following the for loop statement.



Consider the following program that illustrates for loop.
/* program to print number from 1 to 100 */ #include <stdio.h> main (   )
{
int  i ;
for   (i  =   1;   i  <=  100;   i  ++) printf   ("%d \n",   i); } The for loop in C has several capabilities that are not found in loop constructs of other languages. Some of these special features are explained below:
1.        Initialization part: In the initialization part, more than one variable can be
Initialized at a time in the C language for statement as shown below:
Instead of writing  two different statements
t = l;
for (i = 0; i < 17;++i)
one can write a single for statement as:
for (t = 1, i = 0; i < 17;++i)
One can also use expressions in the assignment statements of initialization and increment sections as given below.
for (i(p+q)/2;i>0;i = i/2)

2.        Modification part: The modification part may also have more than one part
as given in the following example:
for (x - 1, y = 50; x < = y; x = x+1, y = y-1)

3.        Condition part: The loop condition may have any compound relation and the
testing need not be limited only to the loop control variable as can be seen in
the following example.
for   (i =1,   sum =  0;   i  <  20   &&   sum <  100;   ++i)
sum =sum + i; printf ("%d \n", sum);
}
The loop is executed as long as both the conditions i < 20 and sum < 100 are true,

4.        Omission of parts: A unique aspect of for loop is that one or more sections
can be omitted, if necessary as shown below.
, x = 5;
for (; m! = 100;)
{
printf ("%d \n", x); x = x+5;
}
Both the initialization and increment sections are omitted in this for statement. The initialization has been done before the for statement and the control variable is incremented inside the loop. We can set up time delay loops using the null statement as follows:
for   (i  =   1000;   i  >  0;   i  =  i-1);
This loop is executed 1000 times without producing any output; it simply causes a time delay.

c programmming online,basic of c language,structure of c programming,c language statements,Compiling a C Program,Pseudo Codes in c,basis data type in c ,variables in C Program,constants in C Program,c operators in C Program,special operators in C ,Decision Making,Branching & Looping in C Program,if statement in C Program,for loop in C Program

No comments:

Post a Comment