developing software applications iteration in visual basic (loops)
TRANSCRIPT
![Page 1: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/1.jpg)
Developing Software Applications
Iteration in Visual Basic(Loops)
![Page 2: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/2.jpg)
Looping
Programs often have a requirement to process a set of instructions several times
This sequence might be for a fixed number of times
Or whether it is repeated might depend on a particular condition
This means that there are a number of choices ………..
![Page 3: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/3.jpg)
Iteration Constructs in VB
For … NextDo … Loop UntilDo While … Loop
![Page 4: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/4.jpg)
Iteration constructs are of 3 types
Repeat a block of code FOR a given number of times
WHILE some condition is true, repeat a block of code
Repeat a block of code UNTIL a condition is true
![Page 5: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/5.jpg)
For loops
The simplest loop
Used when we know exactly how many times we want to repeat things
e.g. Input and add together 20 exam marks
![Page 6: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/6.jpg)
For loops - in structured English
e.g. Input and add together 20 exam marks
For 20 timesInput a markAdd mark to total
![Page 7: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/7.jpg)
For loops - a simple example
e.g. Input and add together 20 exam marks
For iCount = 1 To 20iMark = InputBox(“Enter a mark”)iTotal = iTotal + iMark
Next iCount
![Page 8: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/8.jpg)
Expanding that code ...
Dim iCount, iMark, iTotal as Integer
iTotal = 0For iCount = 1 To 20iMark = InputBox(“Enter a mark”)iTotal = iTotal + iMarkNext iCountMsgBox (“Sum of marks is “ & iTotal)
![Page 9: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/9.jpg)
A fuller syntax of the For loop ..
For <counter> = <start> To <finish> Step <increment>
<statements>Next <counter>
e.g.iSum = 0For iCount = 2 To 100 Step 2
iSum = iSum + iCountNext iCount
Adds the even numbers
2, 4, 6, .. 100
![Page 10: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/10.jpg)
Step can be decreasing …
For iLoopCounter = x To y [Step]Perform functions
Next
For a = 1 To 10For a = 1 To 10 Step 5
For a = 10 To 1 Step -1For a = 10 To 1 Step -5
![Page 11: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/11.jpg)
Example: add the values of numbers 1 to 10
iTotal = 0
For iLoopCounter = 1 to 10
iTotal = iTotal + iLoopCounter
Next
![Page 12: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/12.jpg)
Nested For Loops …
For iCount1 = 1 to 20For iCount2 = 1 to 100
statements …..Next iCount2
Next iCount1
In nested For loops, the Next statement must implicitly state which loop is progressing next
![Page 13: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/13.jpg)
Demonstrating the For loop with a ListBox
Dim iTotal as IntegerDim iCount as Integer
iTotal = 0 For iCount = 1 To 10 iTotal = iTotal + iCount lstNos.AddItem iTotal Next
lblTotal.Caption = iTotal
![Page 14: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/14.jpg)
Question
What about if we want the loop to stop when the total exceeds 100 ?
![Page 15: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/15.jpg)
Stopping the For loop
Dim iTotal as IntegerDim iCount as Integer
iTotal = 0 For iCount = 1 To 100 iTotal = iTotal + iCount lstNos.AddItem iCount & " , " & iTotal If iTotal > 100 Then iCount = 101 End If Next
lblTotal.Caption = iTotal
Setting iCount to a value >100
causes this For loop to stop
![Page 16: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/16.jpg)
trivial example
works, but is not very elegant code
using the IF the statement to force the exit of the FOR loop is not considered to be “good code” … it is bad practice
![Page 17: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/17.jpg)
Do While loop
Do while condition
statement(s)Loop
![Page 18: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/18.jpg)
Back to earlier example
iTotal = 0 iCount = 1
Do While iTotal < 100 iTotal = iTotal + iCount lstNos.AddItem iCount & " , " & iTotal iCount = iCount + 1 Loop
lblTotal.Caption = iTotal
![Page 19: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/19.jpg)
Comparing For with Do While
Dim iTotal, iCount as Integer
iTotal = 0
For iCount = 1 To 100iTotal = iTotal +
iCount lstNos.AddItem iTotal If iTotal > 100 Then iCount = 101 End If Next lblTotal.Caption = iTotal
Dim iTotal, iCount as Integer
iTotal = 0
iCount = 1 Do While iTotal < 100 iTotal = iTotal +
iCount lstNos.AddItem
iTotal iCount = iCount + 1 Loop
lblTotal.Caption = iTotal
![Page 20: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/20.jpg)
Second syntax –Do …….. Loop Until ….
Dim iTotal, iLoopCount as Integer iTotal = 0 iLoopCount = 1
Do iTotal = iTotal + iLoopCount lstNos.AddItem iLoopCount & " , " & iTotal iLoopCount = iLoopCount + 1 Loop Until iTotal >= 100
lblTotal.Caption = iTotal
![Page 21: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/21.jpg)
Comparing the 2 deterministic loops ..
iTotal = 0 iCount = 1
Do While iTotal < 10 iTotal = iTotal +
iCount lstNos.AddItem
iTotal iCount = iCount + 1 Loop
lblTotal.Caption = iTotal
iTotal = 0
iLoopCount = 1
Do iTotal = iTotal +
iLoopCount lstNos.AddItem iTotal iLoopCount = iLoopCount
+ 1 Loop Until iTotal > 10
lblTotal.Caption = iTotalMay look the same … but the results may be different !!
![Page 22: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/22.jpg)
How many times is this loop executed ?
Dim iTotal, iLoopCount As Integer
iTotal = 0 iLoopCount = 1
Do While iTotal < 10 iTotal = iTotal + iLoopCount lstNos.AddItem iLoopCount & " , " & iTotal iLoopCount = iLoopCount + 1 Loop
Label1.Caption = iTotal
![Page 23: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/23.jpg)
How many times is this loop
executed ?
Dim iTotal, iLoopCount As Integer
iTotal = 0 iLoopCount = 1
Do iTotal = iTotal + iLoopCount
lstNos.AddItem iLoopCount & " , " & iTotal iLoopCount = iLoopCount + 1 Loop Until iTotal > 10
lblTotal.Caption = iTotal
![Page 24: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/24.jpg)
Comparing the 2 While loops ..
Do While iTotal < 10
………… …………
Loop
Do
………………………………
Loop Until iTotal > 10
Will not execute at all if iTotal is already 10 or more
Will always execute thecode once, and then testif iTotal is over 10
![Page 25: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/25.jpg)
Summary …
For iCount = 1 To 10…………
Next iCount
Do………….
Loop Until condition
Do While condition………….
Loop
Loops a FIXED NUMBER OF TIMES
ALWAYS EXECUTES CODE ONCEbefore testing whether to loop
TESTS CONDITION FIRST,then executes code if appropriate
![Page 26: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/26.jpg)
Do …. Loop Until ...example
Calculating a factorial
e.g Factorial 6 (mathematically, 6! )
= 6 * 5 * 4 * 3 * 2 * 1
could be coded like this …..
![Page 27: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/27.jpg)
factorial ex. using Do … Loop Until …
Dim iNum, iFactorial as IntegeriFactorial = 1iNum = 6
Do iFactorial = iFactorial * iNumiNum = iNum – 1
Loop Until iNum = 1
Stop when iNumis equal to 1
![Page 28: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/28.jpg)
Seeing how factorial ex. works
Dim iNum, iFactorial as IntegeriFactorial = 1iNum = 6
Do iFactorial = iFactorial *
iNumiNum = iNum – 1
Loop Until iNum = 1
iFactorial1
iNum6
6 5
30 4
120 3
360 2
720 1
![Page 29: Developing Software Applications Iteration in Visual Basic (Loops)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f335503460f94c50962/html5/thumbnails/29.jpg)
Summarising again …
For …
Do … Loop Until …
Do While ….. Loop
Loops a FIXED NUMBER OF TIMES
ALWAYS EXECUTES CODE ONCEbefore testing whether to loop or to stop
TESTS CONDITION FIRST,then executes code if appropriate