Programming Logic and Design Fourth Edition Comprehensive Chapter












































- Slides: 44

Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping Programming Logic and Design, Introductory, Fourth Edition

Objectives • 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 Programming Logic and Design, Introductory, Fourth Edition 2

Objectives (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 Programming Logic and Design, Introductory, Fourth Edition 3

Understanding 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 Programming Logic and Design, Introductory, Fourth Edition 4

Using 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 Programming Logic and Design, Introductory, Fourth Edition 5

Using a while Loop with a Loop Control Variable (continued) Programming Logic and Design, Introductory, Fourth Edition 6

Using 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 Programming Logic and Design, Introductory, Fourth Edition 7

Using a Counter to Control Looping • Developing the application: – The input file Programming Logic and Design, Introductory, Fourth Edition 8

Using a Counter to Control Looping (continued) • Developing the application: – The main loop Programming Logic and Design, Introductory, Fourth Edition 9

Using 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 Programming Logic and Design, Introductory, Fourth Edition 10

Using a Counter to Control Looping (continued) • Developing the application: – housekeeping() module Programming Logic and Design, Introductory, Fourth Edition 11

Using a Counter to Control Looping (continued) • Developing the application: – create. Labels() module Programming Logic and Design, Introductory, Fourth Edition 12

Using a Counter to Control Looping (continued) • Three parts to the loop: – Initialize: Set label. Counter to 0 – Compare: compare label. Counter to 100 – Body: print label. Line and in. First. Name, add 1 to label. Counter • When label. Counter has a value of 100, the loop ends • This loop is executed for each employee record Programming Logic and Design, Introductory, Fourth Edition 13

Using a Counter to Control Looping (continued) • Developing the application: – finish. Up() module Programming Logic and Design, Introductory, Fourth Edition 14

Looping with a Variable Sentinel Value • Developing the application – Print labels based on employee’s production amount – The input file: Programming Logic and Design, Introductory, Fourth Edition 15

Looping with a Variable Sentinel Value (continued) Programming Logic and Design, Introductory, Fourth Edition 16

Looping by Decrementing • Decrementing: counting down • May be more convenient to control a loop by decrementing • May eliminate the need for a counter variable Programming Logic and Design, Introductory, Fourth Edition 17

Avoiding 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 Programming Logic and Design, Introductory, Fourth Edition 18

Neglecting 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 Programming Logic and Design, Introductory, Fourth Edition 19

Neglecting 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 Programming Logic and Design, Introductory, Fourth Edition 20

Using the Wrong Comparison with the Loop Control Variable • Using <= or >= when only < or > was required may cause an extra iteration through the loop Programming Logic and Design, Introductory, Fourth Edition 21

Including 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 Programming Logic and Design, Introductory, Fourth Edition 22

Including Statements Inside the Loop that Belong Outside the Loop (continued) Programming Logic and Design, Introductory, Fourth Edition 23

Initializing 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? Programming Logic and Design, Introductory, Fourth Edition 24

Using 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 Programming Logic and Design, Introductory, Fourth Edition 25

Using 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 Programming Logic and Design, Introductory, Fourth Edition 26

Using the for Statement (continued) • This for statement accomplishes these tasks: – Initializes label. Counter to 0 – Checks label. Counter 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, label. Counter is incremented by 1 and compared to the limit value again Programming Logic and Design, Introductory, Fourth Edition 27

Using 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 Programming Logic and Design, Introductory, Fourth Edition 28

Using 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 Programming Logic and Design, Introductory, Fourth Edition 29

Using the do while and do until Loops (continued) Programming Logic and Design, Introductory, Fourth Edition 30

Using the do while and do until Loops (continued) Programming Logic and Design, Introductory, Fourth Edition 31

Recognizing 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 Programming Logic and Design, Introductory, Fourth Edition 32

Recognizing the Characteristics Shared by All Loops (continued) Programming Logic and Design, Introductory, Fourth Edition 33

Nesting 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: Programming Logic and Design, Introductory, Fourth Edition 34

Nesting Loops (continued) • Developing the application: – The desired output: ¼ of 1% raise in each pay period – Two pay periods per month Programming Logic and Design, Introductory, Fourth Edition 35

Nesting 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 Programming Logic and Design, Introductory, Fourth Edition 36

Nesting Loops (continued) Programming Logic and Design, Introductory, Fourth Edition 37

Nesting Loops (continued) Programming Logic and Design, Introductory, Fourth Edition 38

Using 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 Programming Logic and Design, Introductory, Fourth Edition 39

Using 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 Programming Logic and Design, Introductory, Fourth Edition 40

Using a Loop to Accumulate Totals (continued) Programming Logic and Design, Introductory, Fourth Edition 41

Summary • 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 Programming Logic and Design, Introductory, Fourth Edition 42

Summary (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 Programming Logic and Design, Introductory, Fourth Edition 43

Summary (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 Programming Logic and Design, Introductory, Fourth Edition 44