Title: An Object-Oriented Approach to Programming Logic and Design
1An Object-Oriented Approach to Programming Logic
and Design
2Objectives
- 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
3Objectives (continued)
- Recognize the characteristics shared by all loops
- Nest loops
- Use a loop to accumulate totals
4Understanding 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
5Controlling 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
-
6Using 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
-
7Using a while Loop with a Loop Control Variable
(continued)
8Using a while Loop with a Loop Control Variable
(continued)
9Using 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)
10Using 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
11Using a while Loop with a Loop Control Variable
(continued)
12Using a while Loop with a Loop Control Variable
(continued)
13Using 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
14Using 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
15Using 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
16Using Constant and Variable Sentinel Values
(continued)
17Using Constant and Variable Sentinel Values
(continued)
18Looping by Decrementing
- Decrementing (counting down) a loop control
variable is sometimes more convenient than
incrementing
19Avoiding 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
20Neglecting to Initialize the Loop Control Variable
- Uninitialized variables may contain unknown
values in some languages
21Neglecting to Alter the Loop Control Variable
- Infinite loop a loop that never stops executing
usually caused by failure to alter the loop
control variable
22Using the Wrong Comparison with the Loop Control
Variable
- How many times will each of these loops execute?
counter 0 while counter lt 10 perform
someMethod() counter counter 1 endwhile
counter 0 while counter lt 10 perform
someMethod() counter counter 1 endwhile
23Including Statements Inside the Loop that Belong
Outside the Loop
- Statements that do not need to be repeated should
not be inside a loop
24Using 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
25Using 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
26Using 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
27Recognizing 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
28Recognizing the Characteristics Shared by All
Loops (continued)
29Nesting 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
30Nesting Loops (continued)
31Nesting 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
32Using 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
33Using a Loop to Accumulate Totals (continued)
34Summary
- 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
35Summary (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
36Summary (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
37Summary (continued)
- Nested loops are loops contained within other
loops - Summary reports contain only totals, no detail
records - Accumulator variables are used to accumulate
totals