while () { /*code that is repeated*/ }
The condition has the same form as the condition in an if clause and can also include boolean operators. The sequence of operations is as follows:
1. check the condition and if it is false, terminate the loop (go to step 4) 2. execute the code within the braces 3. go to step 1 and repeat 4. continue execution of the program past the last brace in the while loopAn example while loop is shown below. It performs the same task as the for loop, adding the numbers from 1 to 100:
#include <stdio.h> int main(int argc, char* argv[]) { int i, sum = 0; //must do the initialization before the loop i = 1; while ( i <= 100 ) /*checks the condition and executes the loop while the condition is true.*/ { sum = sum + i; i ++; //must include the update within the loop code } printf("The sum of the numbers from 1 to 100 is: %d\n", sum); return 0; }