An Object-Oriented Approach to Programming Logic and Design
description
Transcript of 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
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
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
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
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
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
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)
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)
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)
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
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:
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:
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
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
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
An Object-Oriented Approach to Programming Logic and Design 16
Using Constant and Variable Sentinel Using Constant and Variable Sentinel Values (continuedValues (continued))
An Object-Oriented Approach to Programming Logic and Design 17
Using Constant and Variable Sentinel Using Constant and Variable Sentinel Values (continuedValues (continued))
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
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
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
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
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
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
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
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
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
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
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)
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
An Object-Oriented Approach to Programming Logic and Design 30
Nesting Loops (continued)Nesting Loops (continued)
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
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
An Object-Oriented Approach to Programming Logic and Design 33
Using a Loop to Accumulate Totals Using a Loop to Accumulate Totals (continued)(continued)
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
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
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
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