Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping."— Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

12 Programming Logic and Design, Introductory, Fourth Edition12 Using a Counter to Control Looping (continued) Developing the application: –createLabels() module

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

14 Programming Logic and Design, Introductory, Fourth Edition14 Using a Counter to Control Looping (continued) Developing the application: –finishUp() module

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


Download ppt "Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping."

Similar presentations


Ads by Google