Lecture 10 Repetition Structures - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Lecture 10 Repetition Structures

Description:

Write pseudocode for the while repetition structure ... Also called trip values or trailer values. 9/5/09. ENGR17: Engineering Programming. 18 ... – PowerPoint PPT presentation

Number of Views:85
Avg rating:3.0/5.0
Slides: 34
Provided by: jimand
Category:

less

Transcript and Presenter's Notes

Title: Lecture 10 Repetition Structures


1
Lecture 10Repetition Structures
  • ENGR17 Engineering Programming
  • Section 1
  • Fall 2001

10/12/01
2
Outline
  • Three Repetition Structures
  • While Loop
  • Write pseudocode for the while repetition
    structure
  • Create a flowchart for the while repetition
    structure
  • Code the while repetition structure
  • Initialize and update counters and accumulators
  • Nested Repetition Structures
  • For Loop
  • for statement to implement a counter-controlled
    loop
  • Do-While Loop
  • Write the pseudocode for the do-while loop
  • Create a flowchart for the do-while loop
  • Code the do-while loop

3
The Repetition Structure
  • Directs the computer to repeat one or more
    instructions until a condition (loop condition)
    is met
  • Also called looping or iteration
  • Three forms
  • while
  • for
  • do-while

4
The Repetition Structure
5
while Repetition Structure
  • Often begins with repeat while and ends with end
    while
  • Loop body instruction(s) between repeat while
    and end while
  • Referred to as a pretest loop or top-driven loop
  • Evaluates loop condition before processing
    statements within the loop
  • Each loop condition results in either a True or a
    False answer only

6
while Loop in Pseudocode
Loop Body
While Loop
7
Repetition Structure Flow Chart
  • Symbol is the Diamond
  • Diamond contains a question that has a True or
    False answer only
  • Question represents the loop condition that the
    repetition structure must evaluate
  • Has two flowlines leaving the symbol
  • Has two flowlines leading into the diamond,
    rather than just one

8
Flow Chart for while Loop
9
Flow Chart for while Loop
10
while Loop in C
  • Use the C while statement

not needed after while
Loop Body
11
while Loop in a Program
12
while Loop in a Function
13
Using Counters and Accumulators
  • Numeric variables used within a repetition
    structure to calculate subtotals, totals, and
    averages
  • Counter
  • Used for counting something
  • Accumulator
  • Used for accumulating (adding together) something
  • Initializing
  • Assigning a beginning value to the counter or
    accumulator
  • Typically done outside the repetition structure
  • Updating (incrementing)
  • Adding a number to the value stored in the
    counter or accumulator
  • Counter incremented by a constant value
  • Accumulator incremented by a value that varies
  • Placed within the repetition structure

14
Accumulator Example
15
The Sales Express Inc. Program
Problem?
16
The Sales Express Inc. Program
17
Sentinel Values
  • Values used to end loops
  • Should be easily distinguishable from the valid
    data used by the program
  • Also called trip values or trailer values

18
Nested Repetition Structures
  • One loop (inner loop), is placed entirely within
    another loop (outer loop)

Outer
Inner
19
(No Transcript)
20
while Loop Example
21
Counter-controlled while Loop
  • Counter-controlled loop
  • Uses a counter to control the number of times the
    loop is processed
  • Uses the value in the counter variable to
    determine if the required number of loop
    repetitions have been performed

22
Using the while Loop
23
Counter-controlled for Loop
  • Allows coding of the loop in a more convenient
    and compact manner

24
for Loop
Use count
25
Tasks Performed by the for Loop
  • Creates and initializes a numeric variable (the
    counter variable) to a beginning value
  • Done only once at the beginning of the loop
  • Evaluates a loop condition to determine if loop
    instructions should be processed
  • If loop condition evaluates to True,loop
    instructions are processed and third task is
    performed otherwise, the loop stops
  • Updates the value in the counter variable

26
for Statement Demo Program
27
for Example
28
The do-while Loop
  • Used to repeat one or more statements either a
    specified number of times or while some loop
    condition is True
  • Often begins with do and ends with end do
  • Is referred to as a posttest loop or
    bottom-driven loop
  • Evaluates the loop condition after first
    processing the loop instructions

29
(No Transcript)
30
The do Loop
31
do-while Loop Example
32
Summary
  • Three Repetition Structures
  • How to use the while form of the repetition
    structure
  • How to use counters and accumulators
  • How to use nested loops
  • How to use the for form of the repetition
    structure
  • How to use the do-while form of the repetition
    structure

33
Example
  • Write 4 functions that print the numbers n to 1
    using
  • While Loop
  • For Loop
  • Do-While Loop
  • Recursion
Write a Comment
User Comments (0)
About PowerShow.com