An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.

23
An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure
  • date post

    15-Jan-2016
  • Category

    Documents

  • view

    228
  • download

    0

Transcript of An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.

Page 1: An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.

An Introduction to Programming with C++

Fifth Edition

Chapter 8More on the Repetition Structure

Page 2: An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.

An Introduction to Programming with C++, Fifth Edition 2

Objectives

• Include the posttest repetition structure in pseudocode

• Include the posttest repetition structure in a flowchart

• Code a posttest loop using the C++ do while statement

• Nest repetition structures

Page 3: An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.

An Introduction to Programming with C++, Fifth Edition 3

Concept Lesson

• Posttest Loops

• Coding the Posttest Loop

• Nested Repetition Structures

Page 4: An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.

An Introduction to Programming with C++, Fifth Edition 4

Posttest Loops

• Loops can be pretest or posttest

• Condition in a posttest loop is evaluated with each loop iteration– Evaluation occurs after instructions within loop are

processed• Also called bottom-driven loops

Page 5: An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.

An Introduction to Programming with C++, Fifth Edition 5

Posttest Loops (continued)

Page 6: An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.

An Introduction to Programming with C++, Fifth Edition 6

Flowcharting a Posttest Loop

• Flowcharts illustrate why loops are referred to as pretest and posttest loops– Repetition diamond appears at the top of a pretest

loop, but at the bottom of a posttest loop

Page 7: An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.

Flowcharting a Posttest Loop (continued)

An Introduction to Programming with C++, Fifth Edition 7

Page 8: An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.

An Introduction to Programming with C++, Fifth Edition 8

Flowcharting a Posttest Loop (continued)

Page 9: An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.

An Introduction to Programming with C++, Fifth Edition 9

Flowcharting a Posttest Loop (continued)

Page 10: An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.

An Introduction to Programming with C++, Fifth Edition 10

Coding the Posttest Loop

• Use the while statement or the for statement to code a pretest loop in C++

• Use the do while statement to code a posttest loop in C++– The loop condition must be a Boolean expression

• Can contain variables, constants, functions, and arithmetic/comparison/logical operators

Page 11: An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.

An Introduction to Programming with C++, Fifth Edition 11

Coding the Posttest Loop (continued)

Page 12: An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.

An Introduction to Programming with C++, Fifth Edition 12

Posttest Loop Example – O’Donnell Incorporated Program

• Problem description– In January of each year, O’Donnell Incorporated

pays a 10% bonus to each of its salespeople– Bonus based on amount of sales made by

salesperson during previous year– Payroll clerk wants a program that calculates and

displays each salesperson’s bonus amount

Page 13: An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.

An Introduction to Programming with C++, Fifth Edition 13

Posttest Loop Example – O’Donnell Incorporated Program (continued)

Page 14: An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.

An Introduction to Programming with C++, Fifth Edition 14

Posttest Loop Example – O’Donnell Incorporated Program

(continued)

Page 15: An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.

An Introduction to Programming with C++, Fifth Edition 15

Nested Repetition Structures

• In a nested repetition structure, one loop (inner loop) is placed entirely within another loop (outer loop)

Page 16: An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.

Nested Repetition Structures (continued)

An Introduction to Programming with C++, Fifth Edition 16

Page 17: An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.

An Introduction to Programming with C++, Fifth Edition 17

Nested Loop Example – Max Beauty Supply Program

• Max Beauty Supply divides its sales territory into two regions: Region 1 and Region 2

• Sales manager wants a program to enter the sales amounts for both regions, one region at a time– Program should calculate the total amount sold in

the current region, and display that information

Page 18: An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.

An Introduction to Programming with C++, Fifth Edition 18

Nested Loop Example – Max Beauty Supply Program (continued)

Page 19: An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.

An Introduction to Programming with C++, Fifth Edition 19

Nested Loop Example – Max Beauty Supply Program (continued)

Page 20: An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.

An Introduction to Programming with C++, Fifth Edition 20

Nested Loop Example – Max Beauty Supply Program (continued)

Page 21: An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.

An Introduction to Programming with C++, Fifth Edition 21

Nested Loop Example – Max Beauty Supply Program (continued)

Page 22: An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.

An Introduction to Programming with C++, Fifth Edition 22

Summary

• A repetition structure can be a pretest or posttest loop– In a pretest loop, the loop condition is evaluated before

the instructions in the loop body are processed• Instructions may never be processed

• Use while or for statements

– In a posttest loop, the loop condition is evaluated after the instructions in the loop body are processed

• Instructions are always processed at least once

• Use the do while statement

• You can nest repetition structures

Page 23: An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.

An Introduction to Programming with C++, Fifth Edition 23

Application Lesson: Using a Nested Repetition Structure in a C++ Program• Lab 8.1: Stop and Analyze

• Lab 8.2– Create a program that displays one or more

multiplication tables for Mrs. Johnson students

• Lab 8.3– Modify program so it uses a posttest loop (instead of

a pretest loop) to display the multiplication tables

• Lab 8.4: Desk-Check Lab

• Lab 8.5: Debugging Lab