Computer Programming TCP1224 Chapter 8 More On Repetition Structure.

24
Computer Programming TCP1224 Chapter 8 More On Repetition Structure

Transcript of Computer Programming TCP1224 Chapter 8 More On Repetition Structure.

Page 1: Computer Programming TCP1224 Chapter 8 More On Repetition Structure.

Computer ProgrammingTCP1224

Chapter 8More On Repetition Structure

Page 2: Computer Programming TCP1224 Chapter 8 More On Repetition Structure.

Repetitive structures

•while and for loops

•Pre-test loops

•Exercises that includes earlier lectures

2

Page 3: Computer Programming TCP1224 Chapter 8 More On Repetition Structure.

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

3

Page 4: Computer Programming TCP1224 Chapter 8 More On Repetition Structure.

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

4

Page 5: Computer Programming TCP1224 Chapter 8 More On Repetition Structure.

Posttest Loops (continued)

5

Page 6: Computer Programming TCP1224 Chapter 8 More On Repetition Structure.

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

6

Page 7: Computer Programming TCP1224 Chapter 8 More On Repetition Structure.

Flowcharting a Pre-test Loop

7

Page 8: Computer Programming TCP1224 Chapter 8 More On Repetition Structure.

Flowcharting a Posttest Loop

8

Page 9: Computer Programming TCP1224 Chapter 8 More On Repetition Structure.

Flowcharting a Posttest Loop

9

Page 10: Computer Programming TCP1224 Chapter 8 More On Repetition Structure.

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

10

Page 11: Computer Programming TCP1224 Chapter 8 More On Repetition Structure.

Coding the Posttest Loop

11

Page 12: Computer Programming TCP1224 Chapter 8 More On Repetition Structure.

Posttest Loop Example

•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

12

Page 13: Computer Programming TCP1224 Chapter 8 More On Repetition Structure.

Posttest Loop Example

13

Page 14: Computer Programming TCP1224 Chapter 8 More On Repetition Structure.

Posttest Loop Example

14

Page 15: Computer Programming TCP1224 Chapter 8 More On Repetition Structure.

Post-test in C++

do{// statements

} while ( <conditions> );

15

Page 16: Computer Programming TCP1224 Chapter 8 More On Repetition Structure.

Nested Repetition Structures

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

16

Page 17: Computer Programming TCP1224 Chapter 8 More On Repetition Structure.

Nested Repetition Structures

17

Page 18: Computer Programming TCP1224 Chapter 8 More On Repetition Structure.

Nested Loop Example• 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

18

Page 19: Computer Programming TCP1224 Chapter 8 More On Repetition Structure.

Nested Loop Example

19

Page 20: Computer Programming TCP1224 Chapter 8 More On Repetition Structure.

Nested Loop Example

20

Page 21: Computer Programming TCP1224 Chapter 8 More On Repetition Structure.

Nested Loop Example

21

Page 22: Computer Programming TCP1224 Chapter 8 More On Repetition Structure.

Nested Loop Example

22

Page 23: Computer Programming TCP1224 Chapter 8 More On Repetition Structure.

Multiple level nestingwhile ( <conditions> ){// statementsfor ( <initialize>; <condition>; <counter> ){

// statementsdo{

// statements} while ( <conditions> );

}}

23

Page 24: Computer Programming TCP1224 Chapter 8 More On Repetition Structure.

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

24