【正文】
Sums and Averages Using a while Loop A First Book of ANSI C, Fourth Edition 17 Computing Sums and Averages Using a while Loop (continued) A First Book of ANSI C, Fourth Edition 18 Computing Sums and Averages Using a while Loop (continued) A First Book of ANSI C, Fourth Edition 19 Computing Sums and Averages Using a while Loop (continued) A First Book of ANSI C, Fourth Edition 20 Ensures that any previous value present in the storage locations assigned to the variable total is overwritten and the total starts at a correct value Accumulating statement Computing Sums and Averages Using a while Loop (continued) A First Book of ANSI C, Fourth Edition 21 Calculating an average Computing Sums and Averages Using a while Loop (continued) A First Book of ANSI C, Fourth Edition 22 Sentinels ? A program, such as Program , can be made much more general by removing the restriction that exactly four numbers are to be entered – The user enters a value for how many numbers will be averaged – You can use a sentinel (a data value used to signal either the start or end of a data series) ? The sentinel values must be selected so as not to conflict with legitimate data values A First Book of ANSI C, Fourth Edition 23 Sentinels (continued) A First Book of ANSI C, Fourth Edition 24 Sentinels (continued) ? One useful sentinel in C is the named constant EOF (End Of File) – The actual value of EOF is pilerdependent, but it is always assigned a code that is not used by any other character – EOF is defined in A First Book of ANSI C, Fourth Edition 25 Sentinels (continued) A First Book of ANSI C, Fourth Edition 26 Sentinels (continued) A First Book of ANSI C, Fourth Edition 27 The break and continue Statements ? A break forces an immediate exit from while, switch, for, and dowhile statements only while(count = 10) { printf(Enter a number: )。A First Book of ANSI C Fourth Edition Chapter 5 Repetition A First Book of ANSI C, Fourth Edition 2 Objectives ? Basic Loop Structures ? The while Statement ? Computing Sums and Averages Using a whil