An Object-Oriented Approach to Programming Logic and Design

37
An Object-Oriented An Object-Oriented Approach to Approach to Programming Logic and Programming Logic and Design Design Chapter 6 Looping

description

An Object-Oriented Approach to Programming Logic and Design. Chapter 6 Looping. Objectives. Understand the advantages of looping Control loops with variables, counters, and sentinel values Avoid common loop mistakes Use a for loop Use a do until loop. Objectives (continued). - PowerPoint PPT Presentation

Transcript of An Object-Oriented Approach to Programming Logic and Design

Page 1: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to An Object-Oriented Approach to

Programming Logic and DesignProgramming Logic and Design

Chapter 6Looping

Page 2: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 2

ObjectivesObjectives

• Understand the advantages of looping• Control loops with variables, counters, and

sentinel values• Avoid common loop mistakes• Use a for loop• Use a do until loop

Page 3: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 3

Objectives (continued)Objectives (continued)

• Recognize the characteristics shared by all loops

• Nest loops• Use a loop to accumulate totals

Page 4: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 4

Understanding the Advantages of Understanding the Advantages of LoopingLooping

• The power of computers is their ability to perform repetitive actions

• Loop: a structure that repeats actions while some condition continues

• Loops allow code that is written only once to be used over and over

Page 5: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 5

Controlling Loops with Variables, Controlling Loops with Variables, Counters and Sentinel ValuesCounters and Sentinel Values

• while loop: asks a question, and performs the actions as long as the answer continues to be True

• To control the number of times a loop repeats, use one of the following:– Loop control variables

– Counters

– Sentinel values

Page 6: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 6

Using a Using a whilewhile Loop with a Loop Loop with a Loop Control VariableControl Variable

• Main loop: controls the overall program logic that is used for every data record to be processed

• Other loops may be contained within the main loop

Page 7: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 7

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

Page 8: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 8

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

Page 9: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 9

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

• To process records in a file, you must:– Open the file: prepares the file to be read

– Read each record, one at a time

– Close the file: makes the file no longer available for reading

• End of file: a condition that allows the program to determine that all of the records have been read (or the file is empty)

Page 10: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 10

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

• Three steps that must occur in every loop:– Provide a starting value to control the loop

– Make a comparison using the controlling value

– Alter the controlling value within the loop

• Loop control variable: variable that determines whether the loop will continue

Page 11: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 11

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

Example without a loop:

Page 12: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 12

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

Example with a loop:

Page 13: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 13

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

• Indefinite (or indeterminate) loop: a loop for which you cannot predict the number of repetitions

• Definite loop: a loop for which you know the exact number of repetitions that will take place

• Loop control decision is always based on a Boolean comparison

• Loop body: the statements that execute within the loop

Page 14: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 14

Using a Counter to Control LoopingUsing a Counter to Control Looping

• Counter: a numeric variable used to count repetitions

• A counter variable may start at any value• Incrementing the counter: adding a value

(usually 1) to the counter variable

Page 15: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 15

Using Constant and Variable Sentinel Using Constant and Variable Sentinel ValuesValues

• Constant sentinel value: a “hard-coded” value that controls the number of loop repetitions

• Variable sentinel value: a variable whose value at run-time will control the number of loop repetitions

Page 16: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 16

Using Constant and Variable Sentinel Using Constant and Variable Sentinel Values (continuedValues (continued))

Page 17: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 17

Using Constant and Variable Sentinel Using Constant and Variable Sentinel Values (continuedValues (continued))

Page 18: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 18

Looping by DecrementingLooping by Decrementing

• Decrementing (counting down) a loop control variable is sometimes more convenient than incrementing

Page 19: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 19

Avoiding Common Loop MistakesAvoiding Common Loop Mistakes

• Most 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

Page 20: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 20

Neglecting to Initialize the Loop Neglecting to Initialize the Loop Control VariableControl Variable

• Uninitialized variables may contain unknown values in some languages

Page 21: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 21

Neglecting to Alter the Loop Control Neglecting to Alter the Loop Control VariableVariable

• Infinite loop: a loop that never stops executing; usually caused by failure to alter the loop control variable

Page 22: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 22

Using the Wrong Comparison with the Using the Wrong Comparison with the Loop Control VariableLoop Control Variable

• How many times will each of these loops execute?

counter = 0

while counter < 10

perform someMethod()

counter = counter + 1

endwhile

counter = 0

while counter <= 10

perform someMethod()

counter = counter + 1

endwhile

Page 23: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 23

Including Statements Inside the Loop Including Statements Inside the Loop that Belong Outside the Loopthat Belong Outside the Loop

• Statements that do not need to be repeated should not be inside a loop

Page 24: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 24

Using a Using a forfor Loop Loop

• Most languages support a for loop• for loop: a definite loop• Use the for loop when you already know how

many repetitions are needed• for statement handles three actions

automatically:– Initialization of loop control variable

– Evaluation of loop condition

– Incrementing or decrementing of loop control variable

Page 25: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 25

Using a Using a forfor Loop (continued) Loop (continued)

• Usual format of a for loop:

for initialValue to finalValue

do something

endfor

• Example:for num = 0 to 99

print “Made for you personally by “,

aWorker.getFirstName()

endfor

Page 26: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 26

Using a Using a do untildo until Loop Loop• Unlike the for and while loops, a do until

loop always executes at least once• The loop condition is checked after the actions

are taken

Page 27: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 27

Recognizing the Characteristics Recognizing the Characteristics Shared by All LoopsShared by All Loops

• All structured loops share these characteristics:– The loop-controlling question provides either

entry to or exit from the repeating structure

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

Page 28: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 28

Recognizing the Characteristics Recognizing the Characteristics Shared by All Loops (continued)Shared by All Loops (continued)

Page 29: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 29

Nesting LoopsNesting Loops

• Nested loops: one loop contained within another loop

• Outer loop: the container loop

• Inner loop: the loop inside the container loop

• Each loop must have a loop control variable that is initialized, tested, and altered

Page 30: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 30

Nesting Loops (continued)Nesting Loops (continued)

Page 31: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 31

Nesting Loops (continued)Nesting Loops (continued)

• Techniques to self-document the program code:

– Choosing variable and constant names that describe their purpose

– Using variables or constants to hold frequently used values that will not change during run-time

Page 32: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 32

Using a Loop to Accumulate TotalsUsing a Loop to Accumulate Totals

• Summary report: contains only counts and totals, not individual records processed

• Accumulator: a variable used to accumulate values during repetitions; a value is added to its current value during each repetition

• Accumulator variable must be initialized prior to entering the loop

Page 33: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 33

Using a Loop to Accumulate Totals Using a Loop to Accumulate Totals (continued)(continued)

Page 34: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 34

SummarySummary

• Loop allows repetition of a set of program statements

• Three steps must occur in every loop:– Initialize the loop control variable

– Compare the loop control variable to a value to determine when the loop stops

– Increment (or decrement) the loop control variable

Page 35: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 35

Summary (continued)Summary (continued)

• Loop control can be done with:– A counter– A constant sentinel value– A variable sentinel value

• Common loop mistakes:– Failing to initialize the loop control variable– Failing to alter the loop control variable– Using the wrong comparison with the loop

control variable– Placing non-repetitive statements inside a loop

Page 36: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 36

Summary (continued)Summary (continued)

• for loop incorporates the three loop steps in a single statement

• do until loop guarantees that the loop body will be executed at least once

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

exit from the repeating structure

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

Page 37: An Object-Oriented Approach to  Programming Logic and Design

An Object-Oriented Approach to Programming Logic and Design 37

Summary (continued)Summary (continued)

• Nested loops are loops contained within other loops

• Summary reports contain only totals, no detail records

• Accumulator variables are used to accumulate totals