Sentinel Based Repetition 59
syntax: The while Loop Statement while (expression) statement a while loop statement is (almost) identical to a for loop statement that has no initialization or increment expressions in theory, for loops and while loops are interchangeable in practice, each loop statement is used for a specific form of repetition 60
Repetition Control Structures two distinct forms of repetition definite repetition implies that the precise number of loop repetitions is known when the loop starts its execution indefinite repetition implies this number is not known sentinel based repetition is one kind of indefinite repetition a sentinel is a special value entered by the user during a loop repetition that indicates end of input data the body of a sentinel based loop is repeatedly executed until the sentinel is entered sentinel based repetition is usually coded with the while loop statement 61
Coding Sentinel Based Repetition priming the loop: obtain first input while (input!= sentinel) { process most recently obtained input obtain next input } the loop and a half construct : while (1) { obtain next input if (input == sentinel) break; process most recently obtained input } 62
Logical Operators Operation Operator or and && not! to determine if diceroll is 7 or 11: diceroll == 7, 11 (incorrect) diceroll == 7 diceroll == 11 (correct) to determine if height is between 11.9 and 12.1: 11.9 < height < 12.1 (incorrect) 11.9 < height && height < 12.1 (correct) 63
Textbook Reading the while loop statement is introduced in section 3.7 sentinel based repetition is covered in section 3.9 section 4.9 discusses the effect of a break statement inside of a loop (ignore the continue statement) logical operators are found in section 4.10 section 4.11 is concerned with the most common error made by C programmers section 4.12 summarizes control structures 64
Wheel of Fortune Program in the TV game show Wheel of Fortune, players try to solve a puzzle consisting of partially revealed text characters the winning player gets to play a bonus round in which the following letters are initially revealed: R, S, T, L, N, and E the program allows the user to enter the solution to the puzzle, and then displays the partially revealed puzzle a player would see 65
Wheel Program: Sample Output 66
Wheel Program: Analysis input: a single line of text control structure: sentinel based repetition the return key ( \n ) actsasthesentinel one character is scanned and processed in each loop repetition output: one character per loop repetition, depending on the input character letters R, S, T, L, N, ande (and spaces) are echoed otherwise, an asterisk (*) is printed 67
Wheel Program: Algorithm print directions repeat the following until the return key is entered scan in a single character test for loop termination print out a character 68
Wheel.c: First Half /* Wheel.c * ======= * This program accepts a single line of text input. * It echos the line revealing the letters * R, S, T, L, N, and E (and spaces), but * replaces all other characters by asterisks ( * ). */ #include <stdio.h> int main() { char ch; /* the most recently character scanned in */ /* print directions */ printf("enter the solution to the puzzle.\n" "The puzzle to be solved by the\n" "player will appear below it.\n\n"); 69
Wheel.c: Second Half while (1) { scanf("%c", &ch); /* input next character */ if (ch == \n ) break; /* test for loop termination */ } /* process character */ if (ch == R ch == S ch == T ch == L ch == N ch == E ch == ) printf("%c", ch); else printf("*"); } return 0; 70