9781285776712_Ch05 - Programming Logic and Design Eighth...

This preview shows page 1 - 13 out of 52 pages.

Programming Logic and DesignEighth EditionChapter 5Looping
ObjectivesIn this chapter, you will learn about:The advantages of loopingUsing a loop control variableNested loopsAvoiding common loop mistakesUsing a forloopCommon loop applicationsThe similarities and differences between selections and loops2Programming Logic and Design, Eighth Edition
Understanding the Advantages of LoopingLooping makes computer programming efficient and worthwhileWrite one set of instructions to operate on multiple, separate sets of dataLess time required for design and codingFewer errorsShorter compile timeLoop: a structure that repeats actions while some condition continues3Programming Logic and Design, Eighth Edition
Understanding the Advantages of Looping (continued)Dual-alternative (or binary) selection structureProvides an action for each of two possible outcomes4Programming Logic and Design, Eighth EditionFigure 5-1The loop structure
Understanding the Advantages of Looping (continued)5Programming Logic and Design, Eighth EditionFigure 5-2The mainline logic common to many business programs
Understanding the Advantages of Looping (continued)6Programming Logic and Design, Eighth EditionQuick Reference 5-1While Statement Pseudocode Standards
Using a Loop Control Variable7Programming Logic and Design, Eighth EditionAs long as a condition remains true, the statements in a whileloop’s body executeControl number of repetitions Loop control variable initialized before entering loopLoop control variable testedBody of loop must alter value of loop control variableRepetitions controlled by:Counter – used to create a definite counter-controlled loopSentinel value – used to create an indefinite loop
8Programming Logic and Design, Eighth EditionUsing a Definite Loop with a CounterDefinite loopExecutes a predetermined number of timesCounter-controlled loopProgram counts loop repetitionsLoop control variables altered by:IncrementingDecrementingCounterAny numeric variable that counts the number of times an event has occurred, usually starts with 0
Using a Definite Loop with a Counter(continued)9Programming Logic and Design, Eighth EditionFigure 5-3A counted whileloop that outputs Hellofour times
Using an Indefinite Loop with a Sentinel Value10Programming Logic and Design, Eighth EditionIndefinite loopPerformed a different number of times each time the program executesThe user decides how many times the loop executesFigure 5-4An indefinite whileloop that displays Helloas long as the user wants to continue
Using an Indefinite Loop with a Sentinel Value (continued)11Programming Logic and Design, Eighth EditionFigure 5-5Typical executions of the program in Figure 5-4 in two environments
Understanding the Loop in a Program’s Mainline Logic12Programming Logic and Design, Eighth Edition

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture