200 likes | 427 Views
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
E N D
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 Text1.text = sum
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
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
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
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
Do Until Loop • Repeats a block of code UNTIL a condition becomes TRUE Do Code to execute Loop Until condition
Example: Dim count, sum as integer count = 1 Do sum = sum + val(Inputbox(“Number”, _ “Enter a Number”) count = count + 1 Loop untilcount > 3
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
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
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
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)
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
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
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")
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
Example of do while and do until using counters and accumulators: sngAvgSales = sngSumSales / intNumSales lblAvg.Caption = Format(sngAvgSales, "currency") End Sub
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")
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 = ""
Example of do while and do until using counters and accumulators: DO UNTIL: sngAvgSales = sngSumSales / intNumSales lblAvg.Caption = Format(sngAvgSales, "currency") End Sub