While Loops:


Here's the syntax:



while (some "condition" is true)
{
    do something
    update the "condition"
}

For example:

#include <stdio.h> void main( ) { int counter, limit; counter = 0; limit = 10; while (counter < limit) { printf("Counter = %d\n", counter); counter++; } printf("\nOut of the loop!\n"); }

Here's the output:

UNIX prompt >>a.out

Counter = 0

Counter = 1

Counter = 2

Counter = 3

Counter = 4

Counter = 5

Counter = 6

Counter = 7

Counter = 8

Counter = 9

Out of the loop!

 

Here's how the loop executes:

 

 

Note:

2 differences between FOR and WHILE loops: