Download presentation
Presentation is loading. Please wait.
Published byMilton Nelson Modified over 9 years ago
1
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping
2
An Object-Oriented Approach to Programming Logic and Design 2 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
3
An Object-Oriented Approach to Programming Logic and Design 3 Objectives (continued) Recognize the characteristics shared by all loops Nest loops Use a loop to accumulate totals
4
An Object-Oriented Approach to Programming Logic and Design 4 Understanding the Advantages of Looping 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
5
An Object-Oriented Approach to Programming Logic and Design 5 Controlling Loops with Variables, Counters 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
6
An Object-Oriented Approach to Programming Logic and Design 6 Using a while Loop with a Loop Control 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
7
An Object-Oriented Approach to Programming Logic and Design 7 Using a while Loop with a Loop Control Variable (continued)
8
An Object-Oriented Approach to Programming Logic and Design 8 Using a while Loop with a Loop Control Variable (continued)
9
An Object-Oriented Approach to Programming Logic and Design 9 Using a while Loop with a Loop 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)
10
An Object-Oriented Approach to Programming Logic and Design 10 Using a while Loop with a Loop 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
11
An Object-Oriented Approach to Programming Logic and Design 11 Using a while Loop with a Loop Control Variable (continued) Example without a loop:
12
An Object-Oriented Approach to Programming Logic and Design 12 Using a while Loop with a Loop Control Variable (continued) Example with a loop:
13
An Object-Oriented Approach to Programming Logic and Design 13 Using a while Loop with a Loop 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
14
An Object-Oriented Approach to Programming Logic and Design 14 Using 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
15
An Object-Oriented Approach to Programming Logic and Design 15 Using Constant and Variable Sentinel Values 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
16
An Object-Oriented Approach to Programming Logic and Design 16 Using Constant and Variable Sentinel Values (continued )
17
An Object-Oriented Approach to Programming Logic and Design 17 Using Constant and Variable Sentinel Values (continued )
18
An Object-Oriented Approach to Programming Logic and Design 18 Looping by Decrementing Decrementing (counting down) a loop control variable is sometimes more convenient than incrementing
19
An Object-Oriented Approach to Programming Logic and Design 19 Avoiding 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
20
An Object-Oriented Approach to Programming Logic and Design 20 Neglecting to Initialize the Loop Control Variable Uninitialized variables may contain unknown values in some languages
21
An Object-Oriented Approach to Programming Logic and Design 21 Neglecting to Alter the Loop Control Variable Infinite loop: a loop that never stops executing; usually caused by failure to alter the loop control variable
22
An Object-Oriented Approach to Programming Logic and Design 22 Using the Wrong Comparison with the Loop 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
23
An Object-Oriented Approach to Programming Logic and Design 23 Including Statements Inside the Loop that Belong Outside the Loop Statements that do not need to be repeated should not be inside a loop
24
An Object-Oriented Approach to Programming Logic and Design 24 Using a for 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
25
An Object-Oriented Approach to Programming Logic and Design 25 Using a for 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
26
An Object-Oriented Approach to Programming Logic and Design 26 Using a do until 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
27
An Object-Oriented Approach to Programming Logic and Design 27 Recognizing the Characteristics Shared 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
28
An Object-Oriented Approach to Programming Logic and Design 28 Recognizing the Characteristics Shared by All Loops (continued)
29
An Object-Oriented Approach to Programming Logic and Design 29 Nesting 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
30
An Object-Oriented Approach to Programming Logic and Design 30 Nesting Loops (continued)
31
An Object-Oriented Approach to Programming Logic and Design 31 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
32
An Object-Oriented Approach to Programming Logic and Design 32 Using 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
33
An Object-Oriented Approach to Programming Logic and Design 33 Using a Loop to Accumulate Totals (continued)
34
An Object-Oriented Approach to Programming Logic and Design 34 Summary 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
35
An Object-Oriented Approach to Programming Logic and Design 35 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
36
An Object-Oriented Approach to Programming Logic and Design 36 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
37
An Object-Oriented Approach to Programming Logic and Design 37 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.