While Loop. .


Loop statements are further divided into 3 types. They are:

—While loop


While(expression) // loop header



} // end of while loop



While starting execution of while loop, the expression specified in loop header is evaluated. If it gives non zero value, the control is transferred to loop body.

After executing the statement of loop body control is transferred to loop header for evaluating the expression.


Again given expression is evaluated & control is transferred to loop body if expression gives non-zero value. i.e. this process is repeated as long as expression value is true


Basic Characteristics-

—Expression is evaluated while starting first iteration as well as before starting each iteration.

—If the expression gives zero value before starting first iteration, loop body statements are not executed.

—In loop header, the expression specified may be

—Single variable

—Any non zero constant

—Any normal expression


Use of single variable-

  • In this case, a variable of any data type can be specified as the condition or expression in loop header.
  • As long as given variable contains non-zero value given loop statement is iterated.



int x =5;

While (x)


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


} //while


Use of normal expression-

—In this case, any simple or complex expression is used as a condition in loop header.

—As long as given expression gives non zero value, loop is iterated.

—E.g. :


int x = 2, y=6;



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




Use of assignment statement in loop header-

—If the expression used is an assignment statement which may be either single or multiple assignment statement, then value of left most variable is used as the result or the condition.

—E.g. :


int x,y=5;



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





int x,y,z,p,q = 5;



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




Use of input functions-

—In this case, value returned by input function is used as the condition in loop header.

—E.g. :


Following loop is iterated as long as value given by gatch function is non-zero.

while(ch = getch())


printf(“\n %c”, ch);

}// while


Following loop is iterated till getting the value 27 which is decoded for ESCAPE key.

while((ch =getch()) !=27)


Printf(“\n %c”, ch);

}// while


Use of complex expression-

—Multiple simple expression are combined by using logical operator to form a complex expression.


int =5, y=1, z=0;

while(x !=y && y&z)

printf(“\n ok”);

}// while





A password will be e-mailed to you.

Feedback Form

Name (required)

Email (required)