Professional Documents
Culture Documents
Repetition - A
Repetition - A
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.)
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
3. Repeat
3. Repeat
• intCnt = intCnt + 1
• intCnt += 1
• intCnt = intCnt – 1
• intCnt -= 1
Counters and Accumulators
• Initializing and
• Updating
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.