1 / 20

Repetition

Repetition. Chapter 7. Overview. For Loop Do Loop Do While Loop Do Until Loop Do Loop While Do Loop Until Nested Loops. Repetition. Computers are good at doing tasks humans find tedious Examples: Determine if the letter 'a' appears in a string

Download Presentation

Repetition

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. Repetition Chapter 7

  2. Overview • For Loop • Do Loop • Do While Loop • Do Until Loop • Do Loop While • Do Loop Until • Nested Loops

  3. Repetition • Computers are good at doing tasks humans find tedious • Examples: • Determine if the letter 'a' appears in a string • Find the sum of the numbers from 1 to 1000000 • Calculate pi to 1000 digits • Calculate monthly loan payments

  4. Types of Loops • Using VB, we can tell a computer to repeat a specific set of instructions • A predetermined number of times • For loop • While (or until) some conditional statement is true • Do loop

  5. For Loop • A For Loop is a structure that is repeated a fixed number of times • We usually select a For Loop when the loop starts at a specific value, increments by a set amount, and terminates at a specific value • The syntax for the For Loop: For LoopCounter = InitialValue to TerminatingValue Program Statement(s) Next LoopCounter

  6. For Loop – Example (1)Default Increment Value of 1 Private Sub CmdOutput_Click() intCounter intSum Dim intCounter as Integer / ? / 0 Dim intSum as Integer / 1 / 1 intSum = 0 / 2 3 / For intCounter = 1 to 5 intSum = intSum + intCounter / 3 / 6 Next intCounter / 4 10 / MsgBox intSum 15 5 End Sub intSum = 15 after the loop is executed

  7. For Loop – Example (2)Incrementing by Values Other than One Private Sub CmdOutput_Click() Dim intCounter as Integer Dim intSum as Integer intSum = 0 For intCounter = 1 to 5 Step 2 intSum = intSum + intCounter Next intSum MsgBox intSum End Sub intCounter intSum ? / 0 / 1 / / 1 / 3 4 / 5 9 intSum = 9 after the loop is executed

  8. For Loop – Example (3)Decrementing the Loop Counter Private Sub CmdOutput_Click() Dim intCounter as Integer Dim intSum as Integer intSum = 0 For intCounter = 5 to 1 Step -2 intSum = intSum + intCounter Next intSum MsgBox intSum End Sub intCounter intSum ? / 0 / / 5 / 5 / 3 8 / 9 1 intSum = 9 after the loop is executed

  9. Do Loops • Do While Loops are used when you are repeating a process that is not controlled by counting with a fixed-step amount, as in a For Loop • Use this construct when your loop will continue while a certain condition evaluates to True • There are four formats that you may use Do Loops with in VB

  10. First Do Loop Construct • This format is used when you wish to test if a condition evaluates to True, before you execute the program statements, and to continue to execute the statement while the condition evaluates to True. Do While (Condition) Program statement(s) Loop

  11. Do While Loop intAnswer = 0 Do While (intAnswer <> vbYes) intAnswer = Msgbox ("Continue?",_ vbYesNo) Loop 'Pretest loop

  12. Second Do Loop Construct • This is used when you wish the loop to execute until a given condition evaluates to True Do Until (Condition) Program statement(s) Loop

  13. Do Until Loop intAnswer = 0 Do Until (intAnswer = vbYes) intAnswer = Msgbox ("Continue?",_ vbYesNo) Loop 'Pretest loop

  14. Third Do Loop Construct • Another form of the Do Loop is used when you wish to execute the program statements at least once and continue to execute the statement while the condition evaluates to True. Do Program statement(s) Loop While (Condition)

  15. Do Loop While Loop Do intAnswer = Msgbox ("Continue?",_ vbYesNo) Loop While (intAnswer <> vbYes) 'Post-test loop

  16. Fourth Do Loop Construct • The final looping construct is used when you wish to execute the program statements at least once, and continue to execute them until the given condition evaluates to True. Do Program statement(s) Loop Until (Condition)

  17. Do Loop Until Loop Do intAnswer = Msgbox ("Continue?",_ vbYesNo) Loop Until (intAnswer = vbYes) 'Post-test loop

  18. Nested Loops • When a loop is contained within the body of another loop, it is said to be nested • Like nesting dolls, hollow wooden dolls that fit one inside of the other • The body of the inner loop is executed many times

  19. Nested Loops intAcc = 0 For intI = 1 to 20 For intJ = 1 to 5 intAcc = intAcc + 1 Next intJ Next intI

  20. Nested Loops intAcc = 0 For intI = 0 to 20 Step 4 For intJ = 100 to 5 Step -16 intAcc = intAcc + 1 Next intJ Next intI

More Related