Title: Programming Logic and Design Fourth Edition, Introductory
1Programming Logic and Design Fourth Edition,
Introductory
2Objectives
- Understand the advantages of looping
- Control a while loop using a loop control
variable - Increment a counter to control a loop
- Loop with a variable sentinel value
- Control a loop by decrementing a loop control
variable
3Objectives (continued)
- Avoid common loop mistakes
- Use a for statement
- Use do while and do until loops
- Recognize the characteristics shared by all loops
- Nest loops
- Use a loop to accumulate totals
4Understanding the Advantages of Looping
- Loop
- Set of instructions that is executed repetitively
based on a condition - Allows processing on large sets of data, such as
complex payroll and benefits processing - Main loop
- Basic set of instructions that is repeated for
every record
5Using a while Loop with a Loop Control Variable
- Loops also occur within program modules
- Three steps in every loop
- Initialize a control variable
- Compare the control variable to a value to
determine if the loop should continue - Alter the control variable within the loop
6Using a while Loop with a Loop Control Variable
(continued)
7Using a while Loop with a Loop Control Variable
(continued)
- Loop control variable determines whether a loop
will continue to execute - Sentinel value a limit or ending value to
compare with the loop control variable - Loop body statements inside the loop that are
executed repetitively - Once the loop body is entered, the entire loop
body must execute - Can exit from a structured loop only at the
comparison test of the loop control variable
8Using a Counter to Control Looping
- Developing the application
- The input file
9Using a Counter to Control Looping (continued)
- Developing the application
- The main loop
10Using a Counter to Control Looping (continued)
- Counter numeric variable that counts how often
an event occurs - Incrementing adding to a variable, usually by 1
- Loop continues executing until the condition is
no longer met
11Using a Counter to Control Looping (continued)
- Developing the application
- housekeeping() module
12Using a Counter to Control Looping (continued)
- Developing the application
- createLabels() module
13Using a Counter to Control Looping (continued)
- Three parts to the loop
- Initialize Set labelCounter to 0
- Compare compare labelCounter to 100
- Body print labelLine and inFirstName, add 1 to
labelCounter - When labelCounter has a value of 100, the loop
ends - This loop is executed for each employee record
14Using a Counter to Control Looping (continued)
- Developing the application
- finishUp() module
15Looping with a Variable Sentinel Value
- Developing the application
- Print labels based on employees production
amount - The input file
16Looping with a Variable Sentinel Value (continued)
17Looping by Decrementing
- Decrementing counting down
- May be more convenient to control a loop by
decrementing - May eliminate the need for a counter variable
18Avoiding 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 - Initializing a variable that does not require
initialization
19Neglecting to Initialize the Loop Control Variable
- Uninitialized variables may contain unknown,
unpredictable garbage - Makes the comparison for the loop test
meaningless - May fail to enter the loop at all
20Neglecting to Alter the Loop Control Variable
- May cause an infinite loop
- Infinite loop a loop that never stops executing
- A structured loop must terminate on its own
21Using the Wrong Comparison with the Loop Control
Variable
- Using lt or gt when only lt or gt was required may
cause an extra iteration through the loop
22Including Statements Inside the Loop that Belong
Outside the Loop
- A statement erroneously placed in a loop will
execute as many times as the loop executes - Affects the performance and efficiency of the
program - Carefully analyze what actions must be repeated,
and place all other actions outside the loop
23Including Statements Inside the Loop that Belong
Outside the Loop (continued)
24Initializing a Variable that Does Not Require
Initialization
- Consider whether the variable requires
initialization - Is an initial value required?
- Will the variable be assigned a value within the
loop?
25Using the for Statement
- Indeterminate (or indefinite) loop when the
number of executions of the loop is not known in
advance - Definite loop when the number of executions of
the loop is known in advance - while statement can be used with both definite
and indefinite loops - for statement can be used with definite loops
26Using the for Statement (continued)
- for statement performs three actions within a
single statement - Initializes the loop control variable
- Evaluates the loop control variable
- Alters the loop control variable (usually by
incrementing) - for statement is a pretest loop
27Using the for Statement (continued)
- This for statement accomplishes these tasks
- Initializes labelCounter to 0
- Checks labelCounter to ensure it is less than or
equal to the limit value 99 - If the evaluation is true, the loop body is
executed - After executing the loop body, labelCounter is
incremented by 1 and compared to the limit value
again
28Using the for Statement (continued)
- Use a for statement when the loop will
- Start with a known starting value
- End with a known ending value
- Increase in equal increments
- Starting, ending and increment values can be
represented with variables - Size of the increment can be set
29Using the do while and do until Loops
- With a pretest loop (for or while), the loop body
may never execute - With a posttest loop, the loop body is always
executed at least once - do while loop continues to execute as long as the
condition remains true - do until loop continues to execute as long as the
condition remains false
30Using the do while and do until Loops (continued)
31Using the do while and do until Loops (continued)
32Recognizing the Characteristics Shared by All
Loops
- All loops share these characteristics
- Loop-controlling question provides either an
entry to or exit from the repeating structure - Loop-controlling question provides the only entry
to or exit from the repeating structure - Structured loops do not allow premature exits
33Recognizing the Characteristics Shared by All
Loops (continued)
34Nesting Loops
- Nesting loops placing one loop inside another
loop - Outer loop a loop that contains another loop
- Inner loop a loop that is inside another loop
- Developing the application
- The input file
35Nesting Loops (continued)
- Developing the application
- The desired output ÂĽ of 1 raise in each pay
period - Two pay periods per month
36Nesting Loops (continued)
- Developing the application
- Use two counters
- One to track months
- One to track the checks within the month
- Use constants to self document the program
- Number of months in the year 12
- Number of checks in the month 2
- Rate of pay increase 0.0025
37Nesting Loops (continued)
38Nesting Loops (continued)
39Using a Loop to Accumulate Totals
- Detail reports show details, may also show
totals or other overall statistics at end - Summary reports show only totals or other
overall statistics - Accumulator a variable used to accumulate
values - Accumulator variable must be initialized (usually
to 0) to ensure it does not contain garbage
40Using a Loop to Accumulate Totals (continued)
- Developing the application
- When finished processing the data file, the
accumulator holds the grand total - Summary can then be printed at end of report
41Using a Loop to Accumulate Totals (continued)
42Summary
- Loop a set of statements that operates on
multiple sets of data - Three steps must occur in a loop initialize,
compare, and alter loop control variable - Counter variable used to count the number of
times an event occurs - Sentinel value can be used to control a loop
43Summary (continued)
- Common loop mistakes
- Failing to initialize, or neglecting to alter the
loop control variable - Using the wrong comparison operator
- Including statements inside the loop that do not
belong there - for statement used with definite loops when you
know the number of times the loop will execute - for statement automatically initializes,
compares, and increments its loop control
variable
44Summary (continued)
- do while and do until loops
- Test the condition at the end of the loop
- Guarantee that the loop body executes at least
once - All structured loops share these characteristics
- Loop controlling question provides either entry
to or exit from the repeating structure - Loop controlling question provides the only entry
to or exit from the repeating structure - Nesting loops loops placed within other loops
- Accumulator variable used to accumulate values