While LOOP in C programming language

While LOOP

   It executes the statement with in the loop until the condition is satisfied. Executes a block of statements as long as a specified condition is TRUE.
 The general syntax for while loop.
   whlie(condition)
   {
     statement;
    }
    statement 1;
The (condition) may be any valid C expression.  The statement(s) may be either a single or a compound (a block of code) C statement.  When while statement encountered, the following events occur:
1. The (condition) is evaluated.
2. If (condition) evaluates to FALSE (zero), the while loop terminates and execution passes to the next_statement.
3. If (condition) evaluates as TRUE (non zero), the C statement(s) is executed.
4. Then, the execution returns to step number 1 until condition becomes FALSE.

ex

#include <stdio.h>
int main(void)
{
int n = 1;
// set the while condition
while(n <= 12)
{
// print
printf("%d ", n);
// increment by 1, repeats
n++;
}
// a newline
printf("\n");
}
Previous
Next Post »