Download presentation
Presentation is loading. Please wait.
Published byLouisa Sutton Modified over 9 years ago
1
Repetition Chapter 7
2
Overview u For Loop u Do Loop Do While Loop Do Until Loop Do Loop While Do Loop Until u Nested Loops
3
Repetition u Computers are good at doing tasks humans find tedious u 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 u 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 u A For Loop is a structure that is repeated a fixed number of times u 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 u 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() Dim intCounter as Integer Dim intSum as Integer intSum = 0 For intCounter = 1 to 5 intSum = intSum + intCounter Next intCounter MsgBox intSum End Sub intCounterintSum 0?/ 1 2 3 4 5 1 3 6 10 15 / / / // // // intSum = 15 after the loop is executed
7
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 intCounterintSum ?0/ 1 / 1/ 3 / 4/ 5 / 9 intSum = 9 after the loop is executed For Loop – Example (2) Incrementing by Values Other than One
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 intCounterintSum ?0/ 5 / 5/ 3 / 8/ 1 / 9 intSum = 9 after the loop is executed
9
Do Loops u 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 u Use this construct when your loop will continue while a certain condition evaluates to True u There are four formats that you may use Do Loops with in VB
10
First Do Loop Construct u 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 u 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 u 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 u 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 u 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 u 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.