Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 106 Computing Fundamentals II Chapter 61 “Loops” Herbert G. Mayer, PSU CS Status 7/29/2013 Initial content copied verbatim from CS 106 material developed.

Similar presentations


Presentation on theme: "1 CS 106 Computing Fundamentals II Chapter 61 “Loops” Herbert G. Mayer, PSU CS Status 7/29/2013 Initial content copied verbatim from CS 106 material developed."— Presentation transcript:

1 1 CS 106 Computing Fundamentals II Chapter 61 “Loops” Herbert G. Mayer, PSU CS Status 7/29/2013 Initial content copied verbatim from CS 106 material developed by CS professors: Cynthia Brown & Robert Martin

2 2 Syllabus Repetition Repetition Real-Life Examples Real-Life Examples Multiplication Table Example Multiplication Table Example For Next Loop For Next Loop Do While Do While Do Until Do Until Nested Loops Nested Loops

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

4 4 Real-Life Examples Keep ringing up items while the customer has moreKeep ringing up items while the customer has more Keep adding numbers until you get to the known end of the listKeep adding numbers until you get to the known end of the list Cut a paycheck for each employee in the rosterCut a paycheck for each employee in the roster 4

5 5 Multiplication Table Example 5

6 6 Printing a Multiplication Table Our job: input a number between 1 and 12 in a text box (txtM)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)Print a multiplication table for this number in a list box (lstAnswer) This is possible but painful with our current set of toolsThis is possible but painful with our current set of tools 6

7 7 Possible code… strM = txtM.Text 'text form of M numM = CInt(strM) 'numeric form of M lstAnswer.Clear‘nothing in the list box 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

8 8 Ugh! This is clumsy and unbearably repetitiveThis 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 codeIf 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 awfulDoing a large number of these would be awful Luckily, VBA has some nice repetition constructsLuckily, VBA has some nice repetition constructs Now for some samples with upper bounds, 4 or 10...Now for some samples with upper bounds, 4 or 10... 8

9 9 For Next loop Repetitions are called loops in VBARepetitions are called loops in VBA Loops go through, what we call, iterations; or we say: loops iterateLoops go through, what we call, iterations; or we say: loops iterate A For Next loop is used when we can determine the number of repetitions before starting the loopA For Next loop is used when we can determine the number of repetitions before starting the loop Such determinations can be computable trivially, if the so called upper bound is a constantSuch determinations can be computable trivially, if the so called upper bound is a constant Or else, if the bound is computable at the latest by the start of the first loop iterationOr else, if the bound is computable at the latest by the start of the first loop iteration 9

10 10 Simple For Next Loop strM = txtM.Text‘ strM = “5” lstAnswer.Clear‘ list box is cleared ’ print the same thing 4 times, i.e. strM For j = 1 To 4‘ iterate 4 times, print same: lstAnswer.AddItem( strM ) Next Prints, for strM = “5” 5555 10

11 11 Another Simple For Next Loop lstAnswer.Clear ‘ prints sequence 1..4 ‘ with implied carriage-return after each: For j = 1 To 4 lstAnswer.AddItem( CStr( j ) ) ‘ convert int Next Prints 4 lines: 1234 11

12 12 For Next Multiplication Table ‘ see line continuation _ in samples: ‘ other line continations after, ( and before ) numM = CInt( txtM.Text ) strM = txtM.Text lstAnswer.Clear For j = 1 To 10 lstAnswer.AddItem(strM & “ x “ & CStr(j) & “ = “ & _ CStr( numM * j ) ) Next Prints, for numM = 5 5 x 1 = 5 5 x 2 = 10... 5 x 10 = 50 12

13 13 Do While Concept Sometimes we can’t tell in advance how many times a loop iteratesSometimes we can’t tell in advance how many times a loop iterates Or, it might just be clearer to use a logic that checks as we go alongOr, it might just be clearer to use a logic that checks as we go along In that case we can use a Do While loop instead of a For Next loopIn that case we can use a Do While loop instead of a For Next loop 13

14 14 Do While Example Use For Next loop program, but this time with a Do While loop numM = CInt(txtM.Text) strM = txtM.Text lstAnswer.Clear j = 1 ‘ set j to initial value 1 at loop start Do While j <= 12‘ upper bund is constant lstAnswer.AddItem( strM & “ x “ & CStr(j) & “ = “ & _ CStr( numM * j ) ) j = j + 1‘ this line makes the loop stop Loop ‘ end with Loop instead of Next 14

15 15 Do While Example Here’s the numM by numM version numM = CInt(txtM.Text)‘ numM is an integer type strM = txtM.Text‘ strM ix a string type lstAnswer.Clear j = 1 Do While j <= numM‘ upper bound is variable lstAnswer.AddItem( strM & “ x “ & CStr( j ) & “ = “ _ & CStr( numM * j ) ) j = j + 1 Loop What happens if numM = 0? 15

16 16 Do While Loop Flowchart Do While condition statements including nested Do While Loop Loop statements can execute forever Loop statements can execute forever Condition true? Execute statements within the loop. Execute statements that follow the loop. No Yes 16

17 17 The Do Until Variation Instead of repeating While a condition is true, we can repeat Until the condition becomes true For example: Do While varA <= 5 Is almost equivalent to: Do Until varA > 5 17

18 18 Test at the End Variation Instead of testing at the beginning of the loop, we can test at the endInstead 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!This is useful because, many times, we want to do the loop code at least once, no matter what! 18

19 19 Do Until Example Multiplication example using Do Until loop with 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 Until j > 12 19

20 20 Do Loop Flowchart (Until, test at end) Dostatement(s) Loop Until condition Do Loop statements always run at least once Do Loop statements always run at least once Condition true? Execute statements within the loop Execute statements that follow the loop No Ye s 20

21 21 Nested Loops Remember how If statements can be “nested”: you can have an If inside another IfRemember 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 loopWe 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 onceIf we do this, the inner loop is executed completely, for all of its repetitions, each time the outer loop is executed once 21

22 22 Nested Loops Dim j, k As Integer lstAnswer.Clear For j = 1 To 12 For k = 1 To 4 lstAnswer.AddItem(“j = “ & CStr( j ) & “ k= “ & CStr( k ) ) Next ‘ k Next ‘ j ‘ how many lines of output?? 22

23 23 Output of Nested Loop 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. 23

24 24 Another Nested Loop Dim j, k As Integer lstAnswer.Clear For j = 1 To 12 For k = 1 To 4 lstAnswer.AddItem(“j = “ & CStr( j ) & “ k= “ & CStr( k ) ) Next ‘k lstAnswer.AddItem(“Finished j = “ & CStr( j ) ) lstAnswer.AddItem(“Finished j = “ & CStr( j ) ) Next ‘j 24

25 25 Results of Another Nested Loop 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. 25


Download ppt "1 CS 106 Computing Fundamentals II Chapter 61 “Loops” Herbert G. Mayer, PSU CS Status 7/29/2013 Initial content copied verbatim from CS 106 material developed."

Similar presentations


Ads by Google