1 / 8

Repetition Structures

Repetition Structures. Yonglei Tao. Counting and Accumulating. Write a program to process input, the number of input values are unknown Sample input 27.50 55.00 38.74 88.26 <empty string> 27.50 63.98 55.00 96.11 38.74 88.26 <empty string> <empty string>. Loop Design. Get a value

orsin
Download Presentation

Repetition Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Repetition Structures Yonglei Tao

  2. Counting and Accumulating • Write a program to process input, the number of input values are unknown • Sample input • 27.50 55.00 38.74 88.26 <empty string> • 27.50 63.98 55.00 96.11 38.74 88.26 <empty string> • <empty string>

  3. Loop Design • Get a value • If it is not the empty string • Process the value • Get a value • If it is not the empty string • Process the value • Get a value • If it is not the empty string • Process the value • Get a value • … • If it is the empty string • Done Get a value Do While it’s not an empty string Process the value Get a value Loop

  4. 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 sales = InputBox (“Enter a sales amount. Click Cancel to end.“, “Sales Entry”) Loop ‘ display the average End Sub

  5. 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) sales = InputBox (“Enter a sales amount. Click Cancel to end.“, “Sales Entry”) Loop ‘ display the average End Sub

  6. 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) sales = InputBox (“Enter a sales amount. Click Cancel to end.“, “Sales Entry”) Loop If ( numSales > 0 ) Then avgSales = totalSales / numSales lblAerage.Text = Format ( avgSales. “Currency”) else lblAerage.Text = “No Sales” end If End Sub

More Related