【正文】
A First Book of ANSI C, Fourth Edition 52 The dowhile Statement (continued) A First Book of ANSI C, Fourth Edition 53 Common Programming Errors ? “Off by one” error, in which the loop executes either one too many or one too few times than intended ? Using the assignment operator, =, instead of the equality operator, ==, in the tested expression ? As with the if statement, repetition statements should not use the equality operator, ==, when testing singleprecision or doubleprecision operands A First Book of ANSI C, Fourth Edition 54 Common Programming Errors (continued) ? Placing a semicolon at the end of the for’s parentheses (creates a donothing loop) ? Using mas to separate the items in a for statement instead of the required semicolons ? Omitting the final semicolon from the do statement A First Book of ANSI C, Fourth Edition 55 Common Compiler Errors A First Book of ANSI C, Fourth Edition 56 Summary ? A section of repeating code is called a loop ? The three C repetition statements are while, for and dowhile ? Loops are also classified as to the type of tested condition ? The most monly used syntax for a while loop is while (expression) { statements。 ? dowhile is a posttest loop ? One type of application is ideally suited for a posttest loop: – Input data validation application A First Book of ANSI C, Fourth Edition 51 The dowhile Statement (continued) do { printf(\nEnter an ID number: )。 ? This statement does not require that any of the items in parentheses be present or that they actually be used for initializing or altering the values in the expression statements – However, the two semicolons must be present ?for ( 。 } A First Book of ANSI C, Fourth Edition 29 The Null Statement ? A semicolon with nothing preceding it is also a valid statement, called the null statement 。grade)。 /* break out of the loop */ } else printf(Keep on truckin!)。 scanf(%f, amp。A First Book of ANSI C Fourth Edition Chapter 5 Repetition A First Book of ANSI C, Fourth Edition 2 Objectives ? Basic Loop