Title: Lecture 10 Repetition Structures
1Lecture 10Repetition Structures
- ENGR17 Engineering Programming
- Section 1
- Fall 2001
10/12/01
2Outline
- 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
3The 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
4The Repetition Structure
5while 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
6while Loop in Pseudocode
Loop Body
While Loop
7Repetition 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
8Flow Chart for while Loop
9Flow Chart for while Loop
10while Loop in C
- Use the C while statement
not needed after while
Loop Body
11while Loop in a Program
12while Loop in a Function
13Using 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
14Accumulator Example
15The Sales Express Inc. Program
Problem?
16The Sales Express Inc. Program
17Sentinel 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
18Nested Repetition Structures
- One loop (inner loop), is placed entirely within
another loop (outer loop)
Outer
Inner
19(No Transcript)
20while Loop Example
21Counter-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
22Using the while Loop
23Counter-controlled for Loop
- Allows coding of the loop in a more convenient
and compact manner
24for Loop
Use count
25Tasks 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
26for Statement Demo Program
27for Example
28The 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)
30The do Loop
31do-while Loop Example
32Summary
- 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
33Example
- Write 4 functions that print the numbers n to 1
using - While Loop
- For Loop
- Do-While Loop
- Recursion