44
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping

Programming Logic and Design Fourth Edition, Comprehensive

  • Upload
    neviah

  • View
    42

  • Download
    1

Embed Size (px)

DESCRIPTION

Programming Logic and Design Fourth Edition, Comprehensive. Chapter 6 Looping. Objectives. Understand the advantages of looping Control a while loop using a loop control variable Increment a counter to control a loop Loop with a variable sentinel value - PowerPoint PPT Presentation

Citation preview

Page 1: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design

Fourth Edition, Comprehensive

Chapter 6Looping

Page 2: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 2

Objectives

• Understand the advantages of looping

• Control a while loop using a loop control variable

• Increment a counter to control a loop

• Loop with a variable sentinel value

• Control a loop by decrementing a loop control variable

Page 3: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 3

Objectives (continued)

• Avoid common loop mistakes

• Use a for statement

• Use do while and do until loops

• Recognize the characteristics shared by all loops

• Nest loops

• Use a loop to accumulate totals

Page 4: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 4

Understanding the Advantages of Looping

• Loop:– Set of instructions that is executed repetitively based

on a condition– Allows processing on large sets of data, such as

complex payroll and benefits processing• Main loop:

– Basic set of instructions that is repeated for every record

Page 5: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 5

Using a while Loop with a Loop Control Variable

• Loops also occur within program modules

• Three steps in every loop:– Initialize a control variable– Compare the control variable to a value to determine if

the loop should continue– Alter the control variable within the loop

Page 6: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 6

Using a while Loop with a Loop Control Variable (continued)

Page 7: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 7

Using a while Loop with a Loop Control Variable (continued)

• Loop control variable: determines whether a loop will continue to execute

• Sentinel value: a limit or ending value to compare with the loop control variable

• Loop body: statements inside the loop that are executed repetitively

• Once the loop body is entered, the entire loop body must execute

• Can exit from a structured loop only at the comparison test of the loop control variable

Page 8: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 8

Using a Counter to Control Looping

• Developing the application:– The input file

Page 9: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 9

Using a Counter to Control Looping (continued)

• Developing the application:– The main loop

Page 10: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 10

Using a Counter to Control Looping (continued)

• Counter: numeric variable that counts how often an event occurs

• Incrementing: adding to a variable, usually by 1

• Loop continues executing until the condition is no longer met

Page 11: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 11

Using a Counter to Control Looping (continued)

• Developing the application:– housekeeping() module

Page 12: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 12

Using a Counter to Control Looping (continued)

• Developing the application:– createLabels() module

Page 13: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 13

Using a Counter to Control Looping (continued)

• Three parts to the loop:– Initialize: Set labelCounter to 0– Compare: compare labelCounter to 100– Body: print labelLine and inFirstName, add 1 to labelCounter

• When labelCounter has a value of 100, the loop ends

• This loop is executed for each employee record

Page 14: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 14

Using a Counter to Control Looping (continued)

• Developing the application:– finishUp() module

Page 15: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 15

Looping with a Variable Sentinel Value

• Developing the application– Print labels based on employee’s production amount– The input file:

Page 16: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 16

Looping with a Variable Sentinel Value (continued)

Page 17: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 17

Looping by Decrementing

• Decrementing: counting down• May be more convenient to control a loop by

decrementing• May eliminate the need for a counter variable

Page 18: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 18

Avoiding Common Loop Mistakes

• Neglecting to initialize the loop control variable

• Neglecting to alter the loop control variable

• Using the wrong comparison with the loop control variable

• Including statements inside the loop that belong outside the loop

• Initializing a variable that does not require initialization

Page 19: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 19

Neglecting to Initialize the Loop Control Variable

• Uninitialized variables may contain unknown, unpredictable garbage

• Makes the comparison for the loop test meaningless

• May fail to enter the loop at all

Page 20: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 20

Neglecting to Alter the Loop Control Variable

• May cause an infinite loop• Infinite loop: a loop that never stops executing• A structured loop must terminate on its own

Page 21: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 21

Using the Wrong Comparison with the Loop Control Variable

• Using <= or >= when only < or > was required may cause an extra iteration through the loop

Page 22: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 22

Including Statements Inside the Loop that Belong Outside the Loop

• A statement erroneously placed in a loop will execute as many times as the loop executes

• Affects the performance and efficiency of the program

• Carefully analyze what actions must be repeated, and place all other actions outside the loop

Page 23: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 23

Including Statements Inside the Loop that Belong Outside the Loop

(continued)

Page 24: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 24

Initializing a Variable that Does Not Require Initialization

• Consider whether the variable requires initialization:– Is an initial value required?– Will the variable be assigned a value within the loop?

Page 25: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 25

Using the for Statement

• Indeterminate (or indefinite) loop: when the number of executions of the loop is not known in advance

• Definite loop: when the number of executions of the loop is known in advance

• while statement can be used with both definite and indefinite loops

• for statement can be used with definite loops

Page 26: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 26

Using the for Statement (continued)

• for statement performs three actions within a single statement:– Initializes the loop control variable– Evaluates the loop control variable– Alters the loop control variable (usually by

incrementing)

• for statement is a pretest loop

Page 27: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 27

Using the for Statement (continued)• This for statement accomplishes these tasks:

– Initializes labelCounter to 0– Checks labelCounter to ensure it is less than or

equal to the limit value 99– If the evaluation is true, the loop body is executed

– After executing the loop body, labelCounter is incremented by 1 and compared to the limit value again

Page 28: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 28

Using the for Statement (continued)

• Use a for statement when the loop will:– Start with a known starting value – End with a known ending value– Increase in equal increments

• Starting, ending and increment values can be represented with variables

• Size of the increment can be set

Page 29: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 29

Using the do while and do until Loops

• With a pretest loop (for or while), the loop body may never execute

• With a posttest loop, the loop body is always executed at least once

• do while loop continues to execute as long as the condition remains true

• do until loop continues to execute as long as the condition remains false

Page 30: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 30

Using the do while and do until Loops (continued)

Page 31: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 31

Using the do while and do until Loops (continued)

Page 32: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 32

Recognizing the Characteristics Shared by All Loops

• All loops share these characteristics:– Loop-controlling question provides either an entry to or

exit from the repeating structure– Loop-controlling question provides the only entry to or

exit from the repeating structure• Structured loops do not allow premature exits

Page 33: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 33

Recognizing the Characteristics Shared by All Loops (continued)

Page 34: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 34

Nesting Loops• Nesting loops: placing one loop inside another loop• Outer loop: a loop that contains another loop• Inner loop: a loop that is inside another loop• Developing the application:

– The input file:

Page 35: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 35

Nesting Loops (continued)

• Developing the application:– The desired output: ¼ of 1% raise in each pay period– Two pay periods per month

Page 36: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 36

Nesting Loops (continued)

• Developing the application:– Use two counters:

• One to track months • One to track the checks within the month

– Use constants to self document the program:• Number of months in the year = 12• Number of checks in the month = 2• Rate of pay increase = 0.0025

Page 37: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 37

Nesting Loops (continued)

Page 38: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 38

Nesting Loops (continued)

Page 39: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 39

Using a Loop to Accumulate Totals

• Detail reports: show details, may also show totals or other overall statistics at end

• Summary reports: show only totals or other overall statistics

• Accumulator: a variable used to accumulate values• Accumulator variable must be initialized (usually to 0)

to ensure it does not contain garbage

Page 40: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 40

Using a Loop to Accumulate Totals (continued)

• Developing the application:– When finished processing the data file, the

accumulator holds the grand total– Summary can then be printed at end of report

Page 41: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 41

Using a Loop to Accumulate Totals (continued)

Page 42: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 42

Summary

• Loop: a set of statements that operates on multiple sets of data

• Three steps must occur in a loop: initialize, compare, and alter loop control variable

• Counter: variable used to count the number of times an event occurs

• Sentinel value can be used to control a loop

Page 43: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 43

Summary (continued)

• Common loop mistakes: – Failing to initialize, or neglecting to alter the loop control

variable– Using the wrong comparison operator– Including statements inside the loop that do not belong

there• for statement: used with definite loops when you

know the number of times the loop will execute• for statement automatically initializes, compares, and

increments its loop control variable

Page 44: Programming Logic and Design Fourth Edition, Comprehensive

Programming Logic and Design, Introductory, Fourth Edition 44

Summary (continued)

• do while and do until loops: – Test the condition at the end of the loop– Guarantee that the loop body executes at least once

• All structured loops share these characteristics:– Loop controlling question provides either entry to or

exit from the repeating structure– Loop controlling question provides the only entry to

or exit from the repeating structure• Nesting loops: loops placed within other loops• Accumulator: variable used to accumulate values