1 / 25

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. CS 106 Computing Fundamentals II Chapter 61 “ Loops ”. Syllabus. Repetition Real-Life Examples Multiplication Table Example

mieko
Download Presentation

CS 106 Computing Fundamentals II Chapter 61 “ 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. 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 CS 106Computing Fundamentals IIChapter 61“Loops”

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

  3. 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 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 session

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

  5. Multiplication Table Example

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

  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 ) )

  8. 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 • Luckily, VBA has some nice repetition constructs • Now for some samples with upper bounds, 4 or 10 . . .

  9. For Next loop • Repetitions are called loops in VBA • Loops 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 loop • Such 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 iteration

  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” • 5 • 5 • 5 • 5

  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: • 1 • 2 • 3 • 4

  12. For NextMultiplication 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

  13. Do While Concept • Sometimes 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 along • In that case we can use a Do While loop instead of a For Next loop

  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

  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?

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

  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

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

  19. Do UntilExample • 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

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

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

  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??

  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.

  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 ) ) • Next ‘j

  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.

More Related