C Language

While Loop

In C Language by Baqir AliLeave a Comment

Situations in which the number of passes through the loop is not known in advance are often best dealt with using a while loop.

The general form of a while statement is:

 while (expression)
   statement ; 

where expression represents a condition that   must be true for the loop to continue execution

The statement is executed repeatedly, as long as the expression is nonzero (i.e., as long as expression is true). The cycle continues until expression becomes zero, at which point execution resumes after while statement.

while (expression)
{
   statement1;
   statement2;
   ...
   statementn;
} 

Of course, the statement must include some feature that eventually alters the value of the expression, thus providing an escape mechanism from the loop.

  • This is not done automatically

When a loop is constructed using a while statement, the test for the continuation of the loop is carried out at the beginning of each pass.

Dissecting a While Loop

Leave a Comment