------------------------------------------------------------ Introducing the While Statement * Note: a while loop is just like an if statement with no "else" branches, with one new feature: repetition. * Example 1: int i; 1 i = 1; 2 while (i <= 5) { 3 printf("This is line %d\n", i); 4 i = i + 1; 5 } 6 i = 100; * Here is the output: This is line 1 This is line 2 This is line 3 This is line 4 This is line 5 * How it works: * CPU reaches line 2 * the Control-Expression is true, so CPU executes the compound statement (lines 3 and 4) * then the CPU jumps back to line 2, and checks the Control-Expression again * still true, so the CPU executes lines 3 and 4 again. * .... and so on ... until * Finally, i is changed to 6 in line 4, so when the processor reaches line 2 again, the Control-Expression is finally false. * At that point, the while loop is done, and the processor jumps to the next statement past the while (line 6). * Note: if we accidentally leave out line 4, then i always has the value of 1 when the Control-Expression in line 2 is evaluated. So the Control-Expression always evaluates to 1 (true), and we keep doing line 3 over and over and over. This is an example of an "Infinite Loop." If your program gets stuck in an infinite loop, type Ctrl-c to stop it. (Hold down the control key, and type c .) * General Form of the While Loop: while ( Control-Expression ) statement; This is how it works. The processor first tests the Control-Expression. If it is non-zero, the statement is executed, and then the expression is evaluated again. And so on. The statement can be a compound statement (it usually is for while loops); if it is, then braces are needed. --------------------------------------------------------- While Loops, Example #2: A loop for reading and testing input from the user. int input; printf("enter an integer from 0 to 10:"); scanf("%d", &input); while (input < 0 || input > 10) { /* while input is bad ... */ printf("please try again\n"); printf("enter an integer from 0 to 10:"); scanf("%d", &input); } ------------------------------------------------------------ A Variation on Example 2: More compact: 1 input = -1; 2 while ( input < 0 || input > 10 ) { /* while input is bad...*/ 3 printf("enter an integer from 0 to 10:"); 4 scanf("%d", &input); 5 } Here, we "artificially" set the input value to -1 initially, because we want the program to execute lines 3 and 4 at least once. (We don't want the Control-Expression to be false when we first come to the while loop.) ------------------------------------------------------------ While Loop, Example 3: A loop for your whole program: #include int main(void) { int proceed; char reply; proceed = 1; while (proceed == 1) { /* here is code to do one iteration of your program */ write a prompt to the screen get input perform a calculation write the result to the screen /*************************************************/ printf("continue? (y/n)-->"); scanf (" %c", &reply); if (reply == 'n') proceed = 0; } } ------------------------------------------------------------