For loop

 

Syntax-

for(initialization; expression ; increment or decrement)

{

// loop body statements

}//end

 

 

Loop header contains 3 sections which are separated by 2 semicolons.

All sections are optional but both semicolons are compulsory.

Initialization section contains one or multiple initialization statements.

Expression section contains simple or complex expressions.

Increment or decrement section contains one or more increment, decrement or normal statements.

The statements specified in initialization section are executed only once while starting the loop execution.

The expression is evaluated before starting each new iteration.

The statements specified in increment & decrement section are executed after each iteration.

Loop is iterated as long as the expression gives true or non zero value.

Multiple loop body statements are specified in begin & end markers.

 

Example:

Specifying all sections

for(i=0;i<10;i++)

{

printf(“\n %d”, i);

}// end

 

Skipping initialization section

 

int i=0;

for(;i<10;i++)

{

printf(“\n %d”,i);

}//end

 

Skipping increment/ decrement section

for(i=0;i<10;)

{

Printf(“\n %d”,i);

i++;

}//end

 

Skipping first & third section

i=0;

for(;i<10;)

{

Printf(‘\n %d “, i);

i++;

}//for

 

Skipping expression section

for(i=0; ;i++)

{

printf(“\n %d “,i);

}//end

 

Skipping all section

i=0;

for(; ;)

{

printf(“\n %d”, i);

i++;

}//end

 

Using Multiple statements in Initialization, Increment & Decrement section-

If multiple statements are to be used in 1st& 3rdsection of FOR loop header, then each statement is separated by comma.

 

E.g. :

for(i=0,j=1;i<j;i++,j++)

{

printf(“\n %d”,i);

}//for

 

For Loop specific to Expression

The expression specified in the loop header is either a single variable or a constant or an expression, which is either arithmetic or relational or shift or logical or bitwise expression.

—Following are the variations of expressions used in FOR loop.

—Using single variable

—Using a constant

—Using none in expression

—Using normal expression

—Use of an assignment statement

—Use of a function

—Using infinite FOR loop

 

 

Using a single variable-

—In this case, as long as the content of specified variable is non-zero, given loop is iterated.

 

for(i=5;i=10;i++)

{

printf(“\n %d”,i);

}// end

 

 

This is an infinite loop as in expression value assigned to I is 1.

 

Using a constant-

—If a non zero constant is used as an expression in loop header given loop goes in infinite iteration.

for(i=5; i ; i++)

{

printf(“\n %d”,i);

}//for

 

 

Using none in expression-

—In this case, if nothing is specified in expression section, by default value used is TRUE.

—Loop goes in infinite iterations.

 

 

Using normal expression-

—If any normal expression is specified in loop header, loop is iterated as long as given expression gives non zero result.

int x=5,y=1;

for(x=5, y=1; x+y; x–)

{

printf(“\n %d”, x+y);

}//for

 

for(x=1,y=5;x<y;x++)

{

printf(“ \n %d”,x);

}//end

 

 

Using assignment statement-

—In an assignment statement the value assigned to left most variable is used as the terminating condition i.e. as long as the value assigned to left most variable is non zero, given loop is iterated.

 

for(x=5; y=x; x–)

{

printf(“ \n %d”, y);

}//for

 

 

Using an input function-

—In this case, output given by specified function is used as a terminating condition.

—As long as output given is non zero, given loop is iterated.

 

for(x=5; sqrt(x); x–)

{

printf (“\n %f %f”,x,sqrt(f));

}//end

 

 

Using infinite FOR loop-

—If the expression is not specified in loop header as well as if break statement is not used in loop body, it goes in infinite iteration.

 

for(x=5; ; x–)

{

printf(“\n %d”, x);

}//end

 

 

 

 

 

 

Registration


A password will be e-mailed to you.

Feedback Form

Name (required)

Email (required)

Feedback