Chapter 5
The Repetition Structure
The Repetition Structure
This section includes the following:
• Repetition
• Counters and accumulators
Repeating Program Instructions
Programmers use the repetition structure(referred to as loop) when they
need the computer to repeatedly process one or more program instructions. If
and how long the instructions are repeated is determined by the loop’s
condition.
The condition is evaluated with each repetition (or iteration) of the loop and can
be phrased in one of two ways:
• It can specify either the requirement for repeating the instructions, referred to
as the looping condition. This indicates that the computer should continue
looping through the instructions.
Repeating Program Instructions …
The requirement for not repeating the instructions is referred to as loop exit
condition, because it tells the computer when to exit (or stop) the loop.
Pretest and Posttest Loops
In a Pretest loop, the condition is evaluated before the instructions within the
loop are processed but in a posttest loop, the condition is evaluated after the
instructions within the loop are processed.
Posttest loops should be used only when you are certain that the loop
instructions should be processed at least once.
The Do …Loop Statement
You can use the Do …Loop statement to code both pretest and posttest loops.
Syntax for a pretest loop
Do {While | Until} condition
loop body instructions which will be processed either
while the condition is true or until the condition becomes true
Loop
Example:
Dim intNumber As Integer = 1
Do While intNumber <= 3
MessageBox.Show(intNumber.Tostring)
intNumber = intNumber + 1
Loop body
The Do …Loop Statement (contd.)
Syntax for a posttest loop
Do
loop body instructions which will be processed either
while the condition is true or until the condition becomes true
Loop {While | Until} condition
Example:
Dim intNumber As Integer = 1
Do
MessageBox.Show(intNumber.Tostring)
Loop body
intNumber = intNumber + 1
Loop Until intNumber > 3
Problem Specification and Pseudocode Containing a Loop
The Quarter of a million club wants an application that allows a club member to
enter two items: the amount of money deposited into a savings account at the
beginning of the year and the annual interest rate. The application should
display the number of years required for the balance in the savings account to
reach one-quarter of a million dollars, assuming the interest is compounded
annually and no withdrawals or additional deposits are made. It also should
display the account balance at that time.
Pseudocode containing the Loop
Version 1: pretest loop
1. Store deposit in balance variable
2. Store rate in rate variable
Looping condition specifies
3. Repeat while balance < 250, 000 when to continue
I. Interest = balance * rate
II. Add interest to balance
III. Add 1 to number of years
IV. End repeat while
4. Display number of years and balance
Pseudocode containing the Loop …
Version 2: pretest loop
1. Store deposit in balance variable
2. Store rate in rate variable
Loop exit condition
3. Repeat until balance >= 250, 000 specifies when to stop
I. Interest = balance * rate
II. Add interest to balance
III. Add 1 to number of years
IV. End repeat until
4. Display number of years and balance
Pseudocode containing the Loop …
Version 3: posttest loop
1. Store deposit in balance variable
2. Store rate in rate variable
3. Repeat
I. Interest = balance * rate
II. Add interest to balance
III. Add 1 to number of years
Looping condition
IV. End repeat while balance < 250,000 specifies when to continue
4. Display number of years and balance
Pseudocode containing the Loop …
Version 4: posttest loop
1. Store deposit in balance variable
2. Store rate in rate variable
3. Repeat
I. Interest = balance * rate
II. Add interest to balance
III. Add 1 to number of years
Loop exit condition
IV. End repeat until balance > = 250,000 specifies when to stop
4. Display number of years and balance
Problem Specification and Pseudocode Containing a Loop …
Problem Specification and Pseudocode Containing a Loop …
Example 2
Example 2 …
Example 2 …
Example 2 …
Counters and Accumulators
• A counter is a numeric variable used for counting something.
• Incrementing - adding a number
• intCnt = intCnt + 1
• intCnt += 1
• Decrementing - subtracting a number •
• intCnt = intCnt – 1
• intCnt -= 1
Counters and Accumulators
Some procedures require you to calculate a subtotal, a total or an average. You
make these calculations using a loop that includes a counter, an accumulator or
both.
A counter is a numeric variable used for counting something such as the
intYears variable in Quarter of a million club application because it
keeps track of the number of years required for the account balance to
reach 250,000.
An accumulator is a numeric variable used for accumulating (adding together)
something such as the dblBalance variable in Quarter of a million club
application is an accumulator because it adds together the annual
interest amount.
Counters and Accumulators (contd.)
Two tasks are associated with counters and accumulators:
• Initializing and
• Updating
Initializing means to assign a beginning value to the counter or accumulator
while
Updating is often referred to as incrementing, means adding a number to the
value stored in the counter or accumulator.
The Sales Express Company Application
The Sales Express Company wants an application that displays the average
amount the company sold during the prior year. The sales manager will enter
the amount of each salesperson’s sales. The application will use a counter to
keep track of the number of sales amounts entered and an accumulator to total
the sales amounts. When the sales manager has finished entering the sales
amounts, the application will calculate the average sales amount by dividing the
value stored in the counter. It then will display the average sales amount. If the
sales manager does not enter any sales amount, the application will display the
“N/A” (for not available)
The Sales Express Company Application Interface
The Sales Express Company Application Code
Priming and Update Read
The priming read is an instruction that appear above the loop. It is use to
prepare or set up the loop. For example, the “get a sales amount from user” in
the sales express company application.
The update read an instruction that appear as last in the body loop. It allows
the user to update the value of the input item. For example, the “get a sales
amount from the user” in the sales express company application.