1 loops zdo while loop: yrepeats a block of code while a condition remains true do while condition...
TRANSCRIPT
![Page 1: 1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d185503460f949ee09c/html5/thumbnails/1.jpg)
1
Loops
Do While Loop: Repeats a block of code WHILE a
condition remains TRUE
Do While conditionCode to execute
Loop
![Page 2: 1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d185503460f949ee09c/html5/thumbnails/2.jpg)
2
Do While Loop (EXAMPLE):Dim count, sum as integercount = 1Do while count <= 3
sum = sum + val(Inputbox(“Number”, _
“Enter a Number”)count = count + 1
LoopText1.text = sum
![Page 3: 1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d185503460f949ee09c/html5/thumbnails/3.jpg)
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
![Page 4: 1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d185503460f949ee09c/html5/thumbnails/4.jpg)
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
![Page 5: 1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d185503460f949ee09c/html5/thumbnails/5.jpg)
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
![Page 6: 1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d185503460f949ee09c/html5/thumbnails/6.jpg)
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_rnd1I2.top = i2.top - li_rnd2
Loop
![Page 7: 1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d185503460f949ee09c/html5/thumbnails/7.jpg)
7
Do Until Loop
Repeats a block of code UNTIL a condition becomes TRUE
Do Code to execute
Loop Until condition
![Page 8: 1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d185503460f949ee09c/html5/thumbnails/8.jpg)
8
Example:Dim count, sum as integercount = 1Do
sum = sum + val(Inputbox(“Number”, _
“Enter a Number”)count = count + 1
Loop until count > 3
![Page 9: 1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d185503460f949ee09c/html5/thumbnails/9.jpg)
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
![Page 10: 1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d185503460f949ee09c/html5/thumbnails/10.jpg)
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
![Page 11: 1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d185503460f949ee09c/html5/thumbnails/11.jpg)
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
![Page 12: 1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d185503460f949ee09c/html5/thumbnails/12.jpg)
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)
![Page 13: 1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d185503460f949ee09c/html5/thumbnails/13.jpg)
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
![Page 14: 1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d185503460f949ee09c/html5/thumbnails/14.jpg)
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
![Page 15: 1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d185503460f949ee09c/html5/thumbnails/15.jpg)
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")
![Page 16: 1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d185503460f949ee09c/html5/thumbnails/16.jpg)
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
![Page 17: 1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d185503460f949ee09c/html5/thumbnails/17.jpg)
17
Example of do while and do until using counters and accumulators:
sngAvgSales = sngSumSales / intNumSales lblAvg.Caption = Format(sngAvgSales,
"currency")
End Sub
![Page 18: 1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d185503460f949ee09c/html5/thumbnails/18.jpg)
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")
![Page 19: 1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d185503460f949ee09c/html5/thumbnails/19.jpg)
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 = ""
![Page 20: 1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d185503460f949ee09c/html5/thumbnails/20.jpg)
20
Example of do while and do until using counters and accumulators:
DO UNTIL:
sngAvgSales = sngSumSales / intNumSales lblAvg.Caption = Format(sngAvgSales,
"currency")
End Sub