【正文】
t A First Book of ANSI C, Fourth Edition 50 The dowhile Statement (continued) ? The general form of the do statement is do statement。 while (expression)。 ? 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: )。 scanf(%f, amp。idNum)。 } while (idNum 1000 || idNum 1999)。 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。 } A First Book of ANSI C, Fourth Edition 57 Summary (continued) ? A for statement performs the same functions as the while statement, but uses a different form ? The for statement is extremely useful in creating countercontrolled loops ? The dowhile statement is used to create posttest loops because it checks its expression at the end of the loop A First Book of ANSI C, Fourth Edition 58