Download presentation
Presentation is loading. Please wait.
Published byCaitlin Rogers Modified over 8 years ago
1
Chapter 7 Loops Programming In Visual Basic.NET Prepared by Johnny Tsui, CIM@VTC
2
2 Loops Repeating a series of instructions Types of Loops Do Use when the number of iterations is unknown For Next Use when the number of iterations known
3
3 Do Loops Ends based on a condition you specify, either Loop While a condition is True Loop Until a condition becomes True Condition can be located at Top of Loop, Pretest Bottom of Loop, Posttest
4
4 Do Loop General Form Do {While |Until} condition Statements to execute Loop OR Do Statements to execute Loop {While | Until} condition Top of Loop Condition, Pretest Bottom of Loop Condition, Posttest
5
5 Do's - When Evaluated Top Evaluation, not guaranteed to run once since evaluated BEFORE running Do While … Loop Do Until … Loop
6
6 Example: intA = 0 Do Until intA = 10 intA = intA + 1 Loop =10?ActionintA Start0 Do UntilNointA + 11 NointA + 12 NointA + 13 NointA + 14 NointA + 15 NointA + 16 NointA + 17 NointA + 18 NointA + 19 NointA + 110 YesExit
7
7 Example: intA = 0 Do While intA < 10 intA = intA + 1 Loop <10?ActionintA Start0 Do UntilYesintA + 11 YesintA + 12 YesintA + 13 YesintA + 14 YesintA + 15 YesintA + 16 YesintA + 17 YesintA + 18 YesintA + 19 YesintA + 110 NoExit
8
8 Do's - When Evaluated Bottom Evaluation, will always run at least one time Do … Loop While Do … Loop Until
9
9 Example: intA = 0 Do intA = intA + 1 Loop Until intA = 10 ActionintA=10 ? Start0 Loop UntilintA + 11No intA + 12No intA + 13No intA + 14No intA + 15No intA + 16No intA + 17No intA + 18No intA + 19No intA + 110Yes Exit
10
10 Example: intA = 0 Do intA = intA + 1 Loop While intA < 10 ActionintA<10 ? Start0 Loop UntilintA + 11No intA + 12No intA + 13No intA + 14No intA + 15No intA + 16No intA + 17No intA + 18No intA + 19No intA + 110Yes Exit
11
11 Do Loops / For Next Loops: Deciding Which To Use Is the number of repetitions known? Is the expression initially true? Must the loop execute once? Top Eval Do Loop Bottom Eval Do LoopFor Next Loop Do Until LoopDo While Loop NO YES NO YES
12
12 For Next Loops Use when you know the number of iterations Uses a numeric counter variable, called Loop Index, to control number of iterations Loop Index is incremented at the bottom of the loop on each iteration Step value can be included to specify the incrementing amount to increment Loop Index, step can be a negative number
13
13 For Next Loop General Form For LoopIndex = InitialValue To TestValue [Step Increment] Statements Next [LoopIndex]
14
14 Example: intA = 0 intB = 0 For intA = 1 To 10 intB = intB + 1 Next intAintA>10 ?intB Start00 For1No1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10No10 Exit11Yes
15
15 Example: intA = 0 intB = 0 For intA = 1 To 10 Step 2 intB = intB + 1 Next intAintA>10 ?intB Start00 For1No1 3 2 5 3 7 4 9 5 Exit11Yes
16
16 Manually Exiting For Next Loops In some situations you may need to exit the loop prematurely Use the Exit For statement inside the loop structure
17
17 Example: intA = 0 intB = 0 For intA = 1 To 10 intB = intB + 1 If intB = 3 Exit For End If Next intAintA>10 ?intBintB=3 ? Start00 For1No1 2 2 3 3Yes Exit
18
18 Exercises Idenifty the statements that are correctly formed and those that have errors. For those with errors, state what is wrong and how to correct it. (a)For decIndex = 3.5 to 6.0, Step 0.5 Next (b)For 4 = 1 to 10 Step 2 Next (c)For intIndex = 100 to 0 Step -25 Next (d)For intIndex = 0 to -10 Step -1 Next (e)For intIndex = 10 to 1 Next Name: Class:
19
19 Exercises How many times will the body of the loop be executed for each of these examples? (a)For intIndex = 1 to 3 (b)For intIndex = 2 to 11 Step 3 (c)For intIndex = 10 to -1 Step -1 (d)For decIndex = 3.0 to 6.0 Step 0.5 (e)For intIndex = 5 to 1 Name: Class:
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.