1 / 20

Loops

Loops. Do While Loop: Repeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop. Do While Loop (EXAMPLE): Dim count, sum as integer count = 1 Do while count <= 3 sum = sum + val(Inputbox(“Number”, _ “Enter a Number”) count = count + 1 Loop

osmond
Télécharger la présentation

Loops

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. Loops • Do While Loop: • Repeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop

  2. Do While Loop (EXAMPLE): Dim count, sum as integer count = 1 Do while count <= 3 sum = sum + val(Inputbox(“Number”, _ “Enter a Number”) count = count + 1 Loop Text1.text = sum

  3. Do While Loops • Starts with Do While statement and ends with the Loop statement • Requires a condition to be checked • Conditions can contain variables, constants, properties, functions, mathmetical operators, relational operators, logical operators

  4. Do While Loops • The loop executes while the condition evaluates to TRUE, the loops terminates when the condition evaluates to FALSE • PRETEST LOOP --- evaluates the condition BEFORE any code is executed

  5. Do While Loops • It is possible for the loop NOT to process any code at all --- if loop is false to start • YOU MUST CONTROL THE INCREMENT OF THE CONDITIONAL EXPRESSION or you can have an endless loop

  6. Do While Loops Example: • Space Race with do while: Do while i1.top > 0 and i2.top > 0 Li_rnd1 = int((50 – 10 + 1) * rnd + 10) Li_rnd2 = int((50 – 10 + 1) * rnd + 10) I1.top = i1.top - li_rnd1 I2.top = i2.top - li_rnd2 Loop

  7. Do Until Loop • Repeats a block of code UNTIL a condition becomes TRUE Do Code to execute Loop Until condition

  8. Example: Dim count, sum as integer count = 1 Do sum = sum + val(Inputbox(“Number”, _ “Enter a Number”) count = count + 1 Loop untilcount > 3

  9. Do Until Loop • Begins with the Do statement and ends with the Loop until Statement • Requires a condition to be checked • Conditions can contain variables, constants, properties, functions, mathmetical operators, relational operators, logical operators

  10. Do Until Loop • The loop executes while the condition evaluates to FALSE, the loops terminates when the condition evaluates to TRUE • POSTTEST LOOP --- evaluates the condition AFTER any code is executed

  11. Do Until Loop • This loop processes the code at least ONCE • YOU MUST CONTROL THE INCREMENT OF THE CONDITIONAL EXPRESSION or you can have an endless loop

  12. Accumulators and Counters (with do while & until loops) • Used to calculate subtotals, totals and averages • Counter --- numeric variable used for counting something (number of employees paid in a week) • accumulator --- numeric value used to adding together something (total dollar amount of a week’s payroll)

  13. Accumulators and Counters (with do while & until loops) • initializing --- means to assign a beginning value to the counter or accumulator (start values are usually ZERO) • incrementing --- means adding to the counter or accumulator

  14. Example of do while and do until using counters and accumulators: • CALCULATE THE AVERAGE SALES ENTERED BY A USER • User clicks “do while” or “do until” CB and a series of input boxes are produced until they cancel out of the input box. The average of the sales entered is displayed on the form

  15. Example of do while and do until using counters and accumulators: DO WHILE: Private Sub cmdWhile_Click() Dim strSales As String Dim intNumSales As Integer 'counter Dim sngSumSales As Single 'accumulator Dim sngAvgSales As Single strSales = InputBox("Enter a sales amount. Click Cancel when finished.", "Input")

  16. Example of do while and do until using counters and accumulators: DoWhile strSales <> "" intNumSales = intNumSales + 1 'update counter sngSumSales = sngSumSales + Val(strSales) 'update accumulator strSales = InputBox("Enter a sales amount. Click Cancel when finished.", "Input") Loop

  17. Example of do while and do until using counters and accumulators: sngAvgSales = sngSumSales / intNumSales lblAvg.Caption = Format(sngAvgSales, "currency") End Sub

  18. Example of do while and do until using counters and accumulators: DO UNTIL: Private Sub cmdUntil_Click() Dim strSales As String Dim intNumSales As Integer 'counter Dim sngSumSales As Single 'accumulator Dim sngAvgSales As Single strSales = InputBox("Enter a sales amount. Click Cancel when finished.", "Input")

  19. Example of do while and do until using counters and accumulators: DO UNTIL: Do intNumSales = intNumSales + 1 'update counter sngSumSales = sngSumSales + Val(strSales) 'update accumulator strSales = InputBox("Enter a sales amount. Click Cancel when finished.", "Input") Loop Until strSales = ""

  20. Example of do while and do until using counters and accumulators: DO UNTIL: sngAvgSales = sngSumSales / intNumSales lblAvg.Caption = Format(sngAvgSales, "currency") End Sub

More Related