An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 2 whole or in part. Posttest Loops
• Repetition structures can be either pretest or posttest
loops • Pretest loop – condition evaluated before instructions are processed • Posttest loop – condition evaluated after instructions are processed • Posttest loop’s instructions are always processed at least once • Pretest loop’s instructions may never be processed
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 3 whole or in part. Posttest Loops (cont’d.)
Figure 8-1 Problem specification, illustrations, and solutions
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 5 whole or in part. Flowcharting a Posttest Loop
• Decision symbol in a flowchart (a diamond)
representing a repetition structure contains the loop condition • Decision symbol appears at the top of a pretest loop, but at the bottom of a posttest loop
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 6 whole or in part. Flowcharting a Posttest Loop (cont’d.)
Figure 8-3 Commission Program’s problem specification and flowcharts
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 7 whole or in part. Flowcharting a Posttest Loop (cont’d.)
Figure 8-3 Commission Program’s problem specification and flowchart
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 8 whole or in part. The do while Statement
• do while statement is used to code posttest loops in
C++ • Syntax: do { one or more statements to be processed one time, and thereafter as long as the condition is true } while (condition); • Some programmers use a comment (such as: //begin loop) to mark beginning of loop
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 9 whole or in part. The do while Statement (cont’d.)
• Programmer must provide loop condition
– Must evaluate to a Boolean value – May contain variables, constants, functions, arithmetic operators, comparison operators, and logical operators • Programmer must also provide statements to be executed when condition evaluates to true • Braces are required around statements if there are more than one
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 10 whole or in part. The do while Statement (cont’d.)
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 12 whole or in part. Nested Repetition Structures
• Like selection structures, repetition structures can be
nested • You can place one loop (the inner, or nested loop) inside another loop (the outer loop) • Both loops can be pretest loops or posttest loops, or the two loops may be different types • Programmer decides whether a problem requires a nested loop by analyzing the problem specification
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 13 whole or in part. Nested Repetition Structures (cont’d.)
Figure 8-6 Problem specification and solution that requires a loop
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 14 whole or in part. Nested Repetition Structures (cont’d.)
Figure 8-7 Modified problem specification and solution that
• Simple program that simulates a clock with minutes and
seconds. • The outer loop represents minutes. • The inner loop (nested loop) represents seconds. • To make it easier to desk-check the instructions, the nested loop uses only three seconds per minute and the outer loop stops after two minutes.
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 16 whole or in part. The Clock Program (cont’d.)
Figure 8-8 Algorithm, code, and a sample run of the Clock Program
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 17 whole or in part. The Clock Program (cont’d.)
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 18 whole or in part. The Car Depreciation Program
• Calculate the depreciation of a car over time.
• Program uses a counter-controlled loop to display the value of a new car at the end of each of five years, using a 15% annual depreciation rate.
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 19 whole or in part. Car Depreciation Program (cont’d.)
Figure 8-11 Beginning of Car Depreciation Program with Problem
• Modify the Car Depreciation Program to use different
depreciation rates. • This modified program displays the value of a new car at the end of each of five years, using annual depreciation rates of 15%, 20%, and 25%.
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 22 whole or in part. Car Depreciation Program (cont’d.)
Figure 8-12 Beginning of the modified Car Depreciation Program
• A repetition structure can be either a pretest loop or a
posttest loop • In a pretest loop, the loop condition is evaluated before the instructions in the loop are processed • In a posttest loop, the evaluation occurs after the instructions within the loop are processed • Use the do while statement to code a posttest loop in C++ • Use either the while statement or the for statement to code a pretest loop in C++
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 26 whole or in part. Summary (cont’d.)
• Repetition structures can be nested, which means one
loop (called the inner or nested loop) can be placed inside another loop (called the outer loop) • For nested repetition structures to work correctly, the entire inner loop must be contained within the outer loop
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 27 whole or in part. Lab 8-1: Stop and Analyze
• Study the program below and answer the questions.
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 28 whole or in part. Lab 8-2: Plan and Create
Figure 8-15 Problem specification, IPO chart, and desk-
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 30 whole or in part. Lab 8-2: Plan and Create (cont’d.)
Figure 8-16 Beginning of the IPO chart information and C++
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 35 whole or in part. Lab 8-3: Modify
• Modify the program in Lab8-2.cpp. Change the outer for
loop to a posttest loop. Save the modified program as Lab8- 3.cpp • Save, run, and test the program.
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 36 whole or in part. Lab 8-4: What’s missing?
• The program is this lab should display the pattern of
numbers shown in Figure 8-19 below. • Follow the instructions for starting C++ and opening the Lab8-4.cpp file. Put the C++ instructions in the proper order, and then determine the one or more missing instructions. • Test the program appropriately.