Download presentation
Presentation is loading. Please wait.
Published byAbner Maxwell Modified over 9 years ago
1
1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop
2
2 zDo 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
3 Do While Loops zStarts 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
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
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
6 Do While Loops Example: zSpace 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
7 Do Until Loop zRepeats a block of code UNTIL a condition becomes TRUE Do Code to execute Loop Until condition
8
8 Example: Dim count, sum as integer count = 1 Do sum = sum + val(Inputbox(“Number”, _ “Enter a Number”) count = count + 1 Loop until count > 3
9
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
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
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
12 Accumulators and Counters (with do while & until loops) zUsed to calculate subtotals, totals and averages zCounter --- numeric variable used for counting something (number of employees paid in a week) zaccumulator --- numeric value used to adding together something (total dollar amount of a week’s payroll)
13
13 Accumulators and Counters (with do while & until loops) zinitializing --- means to assign a beginning value to the counter or accumulator (start values are usually ZERO) zincrementing --- means adding to the counter or accumulator
14
14 Example of do while and do until using counters and accumulators: zCALCULATE THE AVERAGE SALES ENTERED BY A USER zUser 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
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
16 Example of do while and do until using counters and accumulators: Do While 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
17 Example of do while and do until using counters and accumulators: sngAvgSales = sngSumSales / intNumSales lblAvg.Caption = Format(sngAvgSales, "currency") End Sub
18
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
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
20 Example of do while and do until using counters and accumulators: DO UNTIL: sngAvgSales = sngSumSales / intNumSales lblAvg.Caption = Format(sngAvgSales, "currency") End Sub
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.