Download presentation
Presentation is loading. Please wait.
1
Repetition - Counting and Accumulating
Yonglei Tao
2
Flow Chart – Pretest Loop
3
Flow Chart – Posttest Loop
4
Counting and Accumulating
Write a program to process input, the number of input values are unknown Sample input <empty string> <empty string> <empty string>
5
Loop Design Get a value If it is not the empty string
Process the value Get next value … If it is the empty string Done Get a value Do While it’s not an empty string Process the value Get next value Loop
6
Program Version One Private Sub btnCalculate_Click ( … ) Dim sales As String Dim numSales As Integer sales = InputBox (“Enter a sales amount. Click Cancel to end.“, “Sales Entry”) Do While sales <> String.Empty ‘ process the value Loop ‘ display the average End Sub
7
Program Version Two Private Sub btnCalculate_Click ( … ) Dim sales As String Dim numSales As Integer Dim totalSales, avgSales As Currency sales = InputBox (“Enter a sales amount. Click Cancel to end.“, “Sales Entry”) Do While sales <> String.Empty ‘ process the value numSales = numSales + 1 totalSales = totalSales + Val (sales) Loop ‘ display the average End Sub
8
Private Sub btnCalculate_Click ( … ) Dim sales As String Dim numSales As Integer Dim totalSales, avgSales As Currency sales = InputBox (“Enter a sales amount. Click Cancel to end.“, “Sales Entry”) Do While sales <> String.Empty numSales = numSales + 1 totalSales = totalSales + Val (sales) Loop If ( numSales > 0 ) Then avgSales = totalSales / numSales lblAerage.Text = Format ( avgSales. “Currency”) else lblAerage.Text = “No Sales” end If End Sub
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.