200 likes | 399 Views
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
E N D
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 • Find the sum of the numbers from 1 to 1000000 • Calculate pi to 1000 digits • Calculate monthly loan payments
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
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
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
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
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
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
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
Do While Loop intAnswer = 0 Do While (intAnswer <> vbYes) intAnswer = Msgbox ("Continue?",_ vbYesNo) Loop 'Pretest loop
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
Do Until Loop intAnswer = 0 Do Until (intAnswer = vbYes) intAnswer = Msgbox ("Continue?",_ vbYesNo) Loop 'Pretest loop
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)
Do Loop While Loop Do intAnswer = Msgbox ("Continue?",_ vbYesNo) Loop While (intAnswer <> vbYes) 'Post-test loop
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)
Do Loop Until Loop Do intAnswer = Msgbox ("Continue?",_ vbYesNo) Loop Until (intAnswer = vbYes) 'Post-test loop
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
Nested Loops intAcc = 0 For intI = 1 to 20 For intJ = 1 to 5 intAcc = intAcc + 1 Next intJ Next intI
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