1 / 32

Loops

Loops. Repetition. Repetition of the same or similar actions is often needed in process design Three styles of repetition: Repeat for a fixed number of times Repeat as long as (while) a certain condition is true or until a certain condition is true Repeat for every element of a group or set

tyrell
Télécharger la présentation

Loops

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

  2. Repetition • Repetition of the same or similar actions is often needed in process design • Three styles of repetition: • Repeat for a fixed number of times • Repeat as long as (while) a certain condition is true or until a certain condition is true • Repeat for every element of a group or set • We’ll look at the first two types in this session

  3. Real-Life Examples • Keep ringing up items as long as (while) the customer has more • Keep adding numbers until you get to the end of the list • Cut a paycheck for each employee in the roster

  4. A Simple Programming Example:Multiplication Table

  5. Printing a Multiplication Table • Our job: input a number between 1 and 12 in a text box (txtM) • Print a multiplication table for this number in a list box (lstAnswer) • This is possible but painful with our current set of tools

  6. Possible code… strM = txtM.Text'text form of M numM = CInt(strM) 'numeric form of M lstAnswer.Clear lstAnswer.AddItem (strM & " X 1 = " & CStr(numM * 1)) lstAnswer.AddItem (strM & " X 2 = " & CStr(numM * 2)) lstAnswer.AddItem (strM & " X 3 = " & CStr(numM * 3))… lstAnswer. AddItem(strM & " x 12 = " & CStr(numM * 12) )

  7. Ugh! • This is clumsy and unbearably repetitive • If we wanted to change the upper limit in some way (say do up to 8 *8, or 10*10, instead of going to 12 each time), we would need even uglier code • Doing a large number of these would be awful • We couldn’t even do some very natural tasks [well, we actually could, using procedures in an interesting way, but that is another topic] • Luckily, VBA has some nice repetition constructs

  8. ForNext loop • Repetitions are called loops in VBA • A For Next loop is used when we can determine the number of repetitions before starting the loop

  9. A Very Simple For Next Loop strM = txtM.Text lstAnswer.Clear Forj = 1 To 12 lstAnswer.AddItem(strM) Next • Prints, for strM = 5 5 5 5 etc 12 times

  10. Another Simple For Next Loop lstAnswer.Clear Forj = 1 To 12 lstAnswer.AddItem (CStr(j)) Next • Prints 1 2 3 etc up to 12

  11. For Next loop for multiplication table numM = CInt(txtM.Text) strM = txtM.Text lstAnswer.Clear Forj = 1 To 12 lstAnswer.AddItem(strM & “ x “ & CStr(j) & “ = “ & _ CStr(numM * j)) Next • Prints, for numM = 5 5 x 1 = 5 5 x 2 = 10 etc. up to 5 x 12 = 60

  12. Simple Loop Version 2 strM = txtM.Text lstAnswer.Clear For j = 1 TonumM‘ note upper limit is now num lstMult.Items.Add(strM) Next • Prints, for num = 5 5 5 5 5 5 • A total of 5 times

  13. For loop multiplication version 2 • Let’s modify the earlier example so it prints a multiplication table up to numM * numM, instead of going to numM * 12. numM = CInt(txtM.Text) strM = txtM.Text lstAnswer.Clear() For j = 1 TonumM‘the limits can be variables or expressions lstAnswer.AddItem(strM & “ x “ & CStr(j) & “ = “ & _ CStr(numM * j)) Next

  14. For loop for multiplication table 2 numM = CInt(txtM.Text) strM = txtM.Text lstAnswer.Clear For j = 1 TonumM‘variable upper limit lstAnswer.AddItem(strM & “ x “ & CStr(j) & “ = “ & CStr(numM * j)) Next • Prints, for num = 5 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 • A total of 5 lines

  15. For Next Loop Flowchart cVar = sVal ForcVar = sValToeVal statements Next Here cVar is the control variable, sVal is the start value for cVar, and eVal is the end value for cVar yes cVar > eVal? no Execute loop statements Increment cVar

  16. Let’s step through this with an example…(Multiplication Demo)

  17. The Do While Concept • Sometimes we can’t tell in advance how many times a loop will need to execute • Or, it might just be clearer to use a logic that checks as we go along • In that case we can use a DoWhileloop instead of a For Next loop

  18. Do While Example • Let’s try the same program we did with the For loop, but this time with a Do While loop numM = CInt(txtM.Text) strM = txtM.Text lstAnswer.Clear j = 1 ‘set j to its initial value before the loop starts Do While j <= 12 lstAnswer.AddItem(strM & “ x “ & CStr(j) & “ = “ & _ CStr(numM * j)) j = j + 1 ‘this line is essential to make the loop eventually stop Loop ‘end with Loop instead of Next

  19. Do While Example • Here’s the numM by numM version numM = CInt(txtM.Text) strM = txtM.Text lstAnswer.Clear j = 1 Do While j <= numM‘note the upper bound is now a variable lstAnswer.AddItem(strM & “ x “ & CStr(j) & “ = “ & CStr(numM * j)) j = j + 1 Loop • What happens if numM = 0?

  20. Do While Loop Flowchart No Do While condition statement(s) Loop (Loop statements may never be done.) Condition true? Yes Execute statements within the loop. Execute statements that follow the loop.

  21. The Do Until Variation • Instead of repeating While a condition is true, we can repeat Until the condition is true • For example, Do While varA <= 5 Is equivalent to Do Until varA > 5

  22. The Test at the End Variation • Instead of testing at the beginning of the loop, we can test at the end. • This is useful because, many times, we want to do the loop code at least once, no matter what.

  23. Do Until example • Here’s the multiplication example using a Do Until loop with the test at the end numM = CInt(txtM.Text) strM = txtM.Text lstAnswer.Clear j = 1 Do lstAnswer.AddItem(strM & “ x “ & CStr(j) & “ = “ & _ CStr(numM * j)) j = j + 1 Loop Untilj > 12

  24. Do Loop Flowchart (Until, test at end) Do statement(s) Loop Until condition (Loop statements are always done at least once.) No Execute statements within the loop. Condition true? Yes Execute statements that follow the loop.

  25. Let’s check this out in the demo…

  26. Nested Loops • Remember how If statements can be “nested”: you can have an If inside another If • We can also put whatever we want inside a loop, including another loop • If we do this, the inner loop is executed completely, for all of its repetitions, each time the outer loop is executed once.

  27. Simple Example of Nested Loops Dimj,kAs Integer DimtableLineAs String lstAnswer.Clear Forj = 1 To 12 For k = 1 To 4 lstAnswer.AddItem(“j = “ & CStr(j) & “ k= “ & CStr(k)) Next‘k Next‘j

  28. Results of Simple Example j = 1 k = 1 j = 1 k = 2 j = 1 k = 3 j = 1 k = 4 j = 2 k = 1 j = 2 k = 2 j = 2 k = 3 j = 2 k = 4 j = 3 k = 1 etc

  29. Simple Example 2 Dimj,kAs Integer DimtableLineAs String lstAnswer.Clear Forj = 1 To 12 For k = 1 To 4 lstAnswer.AddItem(“j = “ & CStr(j) & “ k= “ & CStr(k)) Next‘k lstAnswer.AddItem(“Finished j = “ & CStr(j)) Next‘j

  30. Results of Simple Example 2 j = 1 k = 1 j = 1 k = 2 j = 1 k = 3 j = 1 k = 4 Finished j = 1 j = 2 k = 1 j = 2 k = 2 j = 2 k = 3 j = 2 k = 4 Finished j = 2 j = 3 k = 1 etc

  31. Example: Complete Multiplication Table Dimj, kAs Integer DimtableLineAs String Forj = 1 To 12 tableLine = “” ‘ the inner For loop builds one line of the table for one value of j For k = 1 To 12 tableLine = tableLine & CStr(j) & “ x “ _ & CStr(k) & “ = “ & CStr(j*k) & “ “ Next‘k lstAnswer.AddItem(tableLine) ‘print one line of the complete table Next‘j

  32. Let’s look at the demo for this.

More Related