370 likes | 485 Views
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).
E N D
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 An Object-Oriented Approach to Programming Logic and Design
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
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 An Object-Oriented Approach to Programming Logic and Design
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 An Object-Oriented Approach to Programming Logic and Design
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 An Object-Oriented Approach to Programming Logic and Design
Using a while Loop with a Loop Control Variable (continued) An Object-Oriented Approach to Programming Logic and Design
Using a while Loop with a Loop Control Variable (continued) An Object-Oriented Approach to Programming Logic and Design
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) An Object-Oriented Approach to Programming Logic and Design
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 An Object-Oriented Approach to Programming Logic and Design
Using a while Loop with a Loop Control Variable (continued) Example without a loop: An Object-Oriented Approach to Programming Logic and Design
Using a while Loop with a Loop Control Variable (continued) Example with a loop: An Object-Oriented Approach to Programming Logic and Design
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 • Definiteloop: 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
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 An Object-Oriented Approach to Programming Logic and Design
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 An Object-Oriented Approach to Programming Logic and Design
Using Constant and Variable Sentinel Values (continued) An Object-Oriented Approach to Programming Logic and Design
Using Constant and Variable Sentinel Values (continued) An Object-Oriented Approach to Programming Logic and Design
Looping by Decrementing • Decrementing (counting down) a loop control variable is sometimes more convenient than incrementing An Object-Oriented Approach to Programming Logic and Design
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 An Object-Oriented Approach to Programming Logic and Design
Neglecting to Initialize the Loop Control Variable • Uninitialized variables may contain unknown values in some languages An Object-Oriented Approach to Programming Logic and Design
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 An Object-Oriented Approach to Programming Logic and Design
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 An Object-Oriented Approach to Programming Logic and Design
Including Statements Inside the Loop that 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
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 An Object-Oriented Approach to Programming Logic and Design
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 An Object-Oriented Approach to Programming Logic and Design
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 An Object-Oriented Approach to Programming Logic and Design
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 An Object-Oriented Approach to Programming Logic and Design
Recognizing the Characteristics Shared by All Loops (continued) An Object-Oriented Approach to Programming Logic and Design
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 An Object-Oriented Approach to Programming Logic and Design
Nesting Loops (continued) An Object-Oriented Approach to Programming Logic and Design
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
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 An Object-Oriented Approach to Programming Logic and Design
Using a Loop to Accumulate Totals (continued) An Object-Oriented Approach to Programming Logic and Design
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 An Object-Oriented Approach to Programming Logic and Design
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
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
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 An Object-Oriented Approach to Programming Logic and Design