1 / 29

Developing Software Applications

Developing Software Applications. Iteration in Visual Basic (Loops). 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

nat
Télécharger la présentation

Developing Software Applications

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. Developing Software Applications Iteration in Visual Basic(Loops)

  2. 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 ………..

  3. Iteration Constructs in VB • For … Next • Do … Loop Until • Do While … Loop

  4. 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

  5. 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

  6. For loops - in structured English • e.g. Input and add together 20 exam marks For 20 times Input a mark Add mark to total

  7. For loops - a simple example • e.g. Input and add together 20 exam marks For iCount = 1 To 20 iMark = InputBox(“Enter a mark”) iTotal = iTotal + iMark Next iCount

  8. Expanding that code ... Dim iCount, iMark, iTotal as Integer iTotal = 0 For iCount = 1 To 20 iMark = InputBox(“Enter a mark”) iTotal = iTotal + iMark Next iCount MsgBox (“Sum of marks is “ & iTotal)

  9. A fuller syntax of the For loop .. For<counter> = <start> To <finish> Step <increment> <statements> Next <counter> e.g. iSum = 0 For iCount = 2 To 100 Step 2 iSum = iSum + iCount Next iCount Adds the even numbers 2, 4, 6, .. 100

  10. Step can be decreasing … For iLoopCounter = x To y [Step] Perform functions Next For a = 1 To 10 For a = 1 To 10Step 5 For a = 10 To 1 Step -1 For a = 10 To 1 Step -5

  11. Example: add the values of numbers 1 to 10 iTotal = 0 For iLoopCounter = 1 to 10 iTotal = iTotal + iLoopCounter Next

  12. Nested For Loops … For iCount1 = 1 to 20 For iCount2 = 1 to 100 statements ….. Next iCount2 Next iCount1 In nested For loops, the Next statement must implicitly state which loop is progressing next

  13. Demonstrating the For loop with a ListBox Dim iTotal as Integer Dim iCount as Integer iTotal = 0 For iCount = 1 To 10 iTotal = iTotal + iCount lstNos.AddItem iTotal Next lblTotal.Caption = iTotal

  14. Question • What about if we want the loop to stop when the total exceeds 100 ?

  15. Stopping the For loop Dim iTotal as Integer DimiCount as Integer iTotal = 0 ForiCount = 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

  16. 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

  17. Do While loop Do whilecondition statement(s) Loop

  18. 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

  19. Dim iTotal, iCount as Integer iTotal = 0 For iCount = 1 To 100 iTotal = iTotal + iCount lstNos.AddItemiTotal 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 Comparing For with Do While

  20. 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 UntiliTotal >= 100 lblTotal.Caption = iTotal

  21. 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 = iTotal Comparing the 2 deterministic loops .. May look the same … but the results may be different !!

  22. 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

  23. 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

  24. Do While iTotal < 10 ………… ………… Loop Do ……………… ……………… Loop Until iTotal > 10 Comparing the 2 While loops .. Will always execute the code once, and then test if iTotal is over 10 Will not execute at all if iTotal is already 10 or more

  25. Summary … Loops a FIXED NUMBER OF TIMES For iCount = 1 To 10 ………… Next iCount Do …………. Loop Until condition Do While condition …………. Loop ALWAYS EXECUTES CODE ONCE before testing whether to loop TESTS CONDITION FIRST, then executes code if appropriate

  26. Do …. Loop Until ...example • Calculating a factorial • e.g Factorial 6 (mathematically, 6! ) = 6 * 5 * 4 * 3 * 2 * 1 could be coded like this …..

  27. factorial ex. using Do … Loop Until … Dim iNum, iFactorial as Integer iFactorial = 1 iNum = 6 Do iFactorial = iFactorial * iNum iNum = iNum – 1 Loop Until iNum = 1 Stop when iNum is equal to 1

  28. Seeing how factorial ex. works Dim iNum, iFactorial as Integer iFactorial = 1 iNum = 6 Do iFactorial = iFactorial * iNum iNum = iNum – 1 Loop Until iNum = 1 iFactorial 1 iNum 6 6 5 4 30 3 120 360 2 1 720

  29. Summarising again … Loops a FIXED NUMBER OF TIMES For … Do … Loop Until … Do While ….. Loop ALWAYS EXECUTES CODE ONCE before testing whether to loop or to stop TESTS CONDITION FIRST, then executes code if appropriate

More Related