Chapter 8: More on the Repetition Structure Introduction to Programming with C++ Fourth Edition
Objectives Show the posttest repetition structure in pseudocode and in a flowchart Code a posttest loop using the C++ do while statement Nest repetition structures Introduction to Programming with C++, Fourth Edition
Posttest Loops Condition is evaluated with each repetition, or iteration, of the loop Evaluation occurs after the instructions within the loop are processed Introduction to Programming with C++, Fourth Edition
Posttest Loops (continued) Introduction to Programming with C++, Fourth Edition
Coding the Posttest Loop In the do while statement, the programmer must supply the loop condition to be evaluated Loop condition: Must be a Boolean expression (true or false) Can contain variables, constants, functions, methods, arithmetic operators, comparison operators, and logical operators Introduction to Programming with C++, Fourth Edition
Coding the Posttest Loop (continued) Programmer must also supply statements to be processed when the loop condition evaluates to true If more than one statement needs to be processed, they must be entered as a statement block Introduction to Programming with C++, Fourth Edition
Syntax of the C++ do while Statement Introduction to Programming with C++, Fourth Edition
Nested Repetition Structures Inner loop - placed entirely within another loop, called the outer loop Introduction to Programming with C++, Fourth Edition
Nested Loops Used by a Clock Introduction to Programming with C++, Fourth Edition
Summary Posttest repetition structure evaluates a condition after the loop body Use the do while statement to code a posttest loop You can also use nested repetition structures Make sure the outer loop completely contains the inner loop Introduction to Programming with C++, Fourth Edition