Programming Logic and Design, Fifth Edition, Introductory5-1

Chapter 5

Looping

At a Glance

  • Overview
  • Objectives
  • Teaching Tips
  • Quick Quizzes
  • Class Discussion Topics
  • Additional Projects
  • Additional Resources
  • Key Terms

Lecture Notes

Overview

Chapter 5coversloops in detail. Students learn the advantages of looping. They also learn to use a while loop to read and process records from a data file, to control loops with sentinel values, and to use decrementing to control loops. Students are also introduced to the for loop, the dowhile loop, and the dountil loop. Finally, students learn how to use nested loops and how to accumulate totals using loops.

Chapter Objectives

  • Learn about the advantages of looping
  • Control loops with counters and sentinel values
  • Nest loops
  • Learn to avoid common loop mistakes
  • Use a for statement
  • Use posttest loops
  • Recognize the characteristics shared by all loops
  • Learn about common loop applications

Teaching Tips

Understanding the Advantages of Looping

  1. Walk through the bulleted list of tasks on page 194, and point out how each might require looping.
  1. Discuss the advantages of using loops in a program.
  1. Describe several examples in which looping might be required, such as processing payroll records, processing orders, checking stock inventory, etc.

Controlling Loops with Counters and Sentinel Values

  1. Remind students of the while loop structure from Chapter 2.
  1. Point out that the number of repetitions in a loop must be controlled or the loop will be an infinite loop.

Using a Definite while Loop with a Counter

  1. Walk through the bulleted list of actions that should occur to make a while loop end correctly on page 195.
  1. Walk through the code example in Figure 5-2, pointing out the loop control variable and explaining how it is used to control the number of repetitions of the loop.
  1. Explain how to control a loop by incrementing or decrementing control variables.
  1. Discuss how the example in Figure 5-2 could be controlled by decrementing the control variable.
  1. Explain what a definite or counted loop is, and discuss the concept of a counter.
  1. Emphasize that although many loops start with the counter set to zero, it is not required to do so.

Using an Indefinite while Loop with a Sentinel Value

  1. Walk through the examples in Figures 5-3 and 5-4, pointing out how the loop is controlled by user input.
  1. Explain what an indefinite while loop is.
  1. Walk through the itemized list on page 199, which describes the three steps that must occur in every loop.
  1. Explain what a loop control variable is.

Quick Quiz 1

  1. What are the three steps found in every loop?

Answer: initialize the loop control variable, compare the loop control variable, alter the loop control variable

  1. When can a structured loop be exited?

Answer: at the comparison test

  1. Another term for “counting down” is ______.

Answer: decrementing

Nested Loops

  1. Introduce the concept of nesting loops.
  1. Introduce the terms inner and outer loop.
  1. Walk through the sample application, with emphasis on the use of separate counters to control each loop.

Mixing Constant and Variable Sentinel Values

  1. Explain that the number of times a loop executes may depend on a constant or on a variable that changes, and walk through the example in Figure 5-8.

Avoiding Common Loop Mistakes

  1. State the fourmost common loop mistakes.

Mistake:Neglecting to Initialize the Loop Control Variable

  1. Remind students that an uninitialized loop control variable may contain garbage that would cause the loop to fail to execute or execute an incorrect number of times.
  1. Walk through the example in Figure 5-10.

Mistake: Neglecting to Alter the Loop Control Variable

  1. Remind students that the loop control variable’s value is the key factor in determining how many times the loop will execute.
  1. Discuss the possibility of an infinite loop, and walk through the example in Figure 5-11.

Mistake: Using the Wrong Comparison With the Loop Control Variable

  1. Walk through the two samples of code on page 212, and ensure that students understand the difference in the results that are produced with each sample.
  1. Discuss the possible repercussions of performing a loop one time too many or too few.

Mistake: Including Statements Inside the Loop that Belong Outside the Loop

  1. Point out that while this error may not actually cause incorrect results, it potentially degrades the performance of the program.
  1. Walk through the examples in Figures 5-12, 5-13, and 5-14.

Using the forLoop

  1. Remind students ofthe concepts of definite and indefinite loops.
  1. Introduce the for statement as a structure for definite loops.
  1. Discuss the three actions that are performed automatically within the for statement.
  1. Discuss the sample for loop code on page 216 in detail.
  1. Walk through the bulleted list of tasks accomplished by the for loop on page 216.
  1. Point out the conditions that suggest the use of a for loop.
  1. Describe the use of variables for the increment, starting, and ending values of a for loop.
  1. Explain what a step value is.

Using Posttest Loops

  1. Remind students that when using a while or a for loop, the body of the loop may never execute.
  1. Remind students that to ensure the loop body executes at least one time, use a posttest loop. Refer them to Chapter 2 if they need a refresher on posttest loops.
  1. Introduce the do-while loop, and compare it with the while loop.
  1. Introduce the do-until loop, and compare it with both the while loop and the do-while loop.
  1. Walk through the examples shown in Figure 5-15 and 5-16,and point out the differences in the code based on which type of loop is used.

Quick Quiz 2

  1. If the loop control variable is not altered within the loop, what will happen?

Answer: An infinite loop will occur.

  1. If the loop control variable is not initialized, what may happen?

Answer: The loop may not execute at all, or may execute an incorrect number of times.

  1. In a for loop, is the condition tested before or after the loop body is executed?

Answer: before

  1. In a do-until loop, does execution of the loop body repeat when the condition is true or false?

Answer: false

Recognizing the Characteristics Shared by All Loops

  1. Point out that you could solve every problem using a while loop.
  1. Explain that all structured loops share two characteristics:
  2. The loop-controlling question must provide either entry to or exit from the repeating structure.
  3. The loop-controlling question provides the only entry to or exit from the repeating structure.
  1. Walk through the examples in Figure 5-17 to discuss unstructured loops.

Common Loop Applications

  1. Mention that loops are frequently used to accumulate totals and validate data.

Using a Loop to Accumulate Totals

  1. Introduce the concept of a summary report or summary section on a report.
  1. Introduce the concept of an accumulator variable.
  1. Stress the importance of initializing the accumulator variable.
  1. Walk through the example in Figure 5-19.

Using a Loop to Validate Data

  1. Explain that validating data ensures that it falls within an acceptable range.
  1. Walk through the bulleted list of possible actions on page 224 and the example in Figure 5-20.
  1. Point out the error in logic in the example in Figure 5-20 and show how to correct the error in Figure 5-21.

Quick Quiz 3

  1. You have an outer for loop that will execute five times, with an inner for loop that will execute three times each time it is entered. How many times in total will the inner for loop execute?

Answer: 15 (three times in each of the five iterations of the outer loop)

  1. A report that contains only totals and other overall statistical information is called a(n) _____ report.

Answer: summary

  1. A variable that is used to hold a running total is called a(n) ______variable.

Answer: accumulator

  1. looping to parse a sentence (or command statements), and sorting sets of data.

Additional Resources

  1. Structured programming and looping with JavaScript:


  1. For loops compared in several languages:


  1. Loops and other structures:


  1. Counter-controlled loops and sentinel-controlled loops:

Key Terms

accumulator—A variable that you use to gather or accumulate values.

counted loop—A loop for which the number of repetitions is a predetermined value.

counter—A numeric variable you use to count the number of times an event has occurred.

decrementing—To change a variable by decreasing it by a constant value, frequently 1. Contrast with incrementing.

definite loop—A loop for which the number of repetitions is a predetermined value.

for statement—A statement that can be used tocode definite loops. It contains a loop control variable that it automatically initializes, evaluates, and increments. Also called a for loop.

incrementing—To change a variable by adding a constant value to it, frequently 1. Contrast with decrementing.

indefinite loop—A loop for which you cannot predetermine the number of executions.

inner loop—A loop that contains a nested, outer loop.

loop—A structure that repeats actions while a condition continues.

loop control variable—A variable that determines whether a loop will continue.

nested loop—A loop structure within another loop structure; nesting loops are loops within loops. See also inner loop and outer loop.

outer loop—A loop that contains a nested, inner loop.

step value—A number you use to increase a loop control variable on each pass through a loop.

summary report—A report that does not include any information about individual records, but instead includes only group totals.

validating data—To make sure data falls within acceptable ranges.

validating input—The process of checking a user’s responses to ensure they fall within acceptable bounds.