chapter 7 loops programming in visual basic.net prepared by johnny tsui, cim@vtc

19
Chapter 7 Loops Programming In Visual Basic.NET Prepared by Johnny Tsui, CIM@VTC

Upload: caitlin-rogers

Post on 04-Jan-2016

216 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Chapter 7 Loops Programming In Visual Basic.NET Prepared by Johnny Tsui, CIM@VTC

Chapter 7Loops

Programming InVisual Basic.NET

Prepared by Johnny Tsui, CIM@VTC

Page 2: Chapter 7 Loops Programming In Visual Basic.NET Prepared by Johnny Tsui, CIM@VTC

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

Page 3: Chapter 7 Loops Programming In Visual Basic.NET Prepared by Johnny Tsui, CIM@VTC

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

Page 4: Chapter 7 Loops Programming In Visual Basic.NET Prepared by Johnny Tsui, CIM@VTC

4

Do Loop General Form

Do {While |Until} conditionStatements to execute

Loop

OR

DoStatements to execute

Loop {While | Until} condition

Top of Loop Condition,

Pretest

Bottom of Loop Condition,

Posttest

Page 5: Chapter 7 Loops Programming In Visual Basic.NET Prepared by Johnny Tsui, CIM@VTC

5

Do's - When Evaluated Top Evaluation, not guaranteed to run

once since evaluated BEFORE runningDo While … LoopDo Until … Loop

Page 6: Chapter 7 Loops Programming In Visual Basic.NET Prepared by Johnny Tsui, CIM@VTC

6

Example:intA = 0Do Until intA = 10

intA = intA + 1Loop

=10? Action intA

Start 0

Do Until No intA + 1 1

No intA + 1 2

No intA + 1 3

No intA + 1 4

No intA + 1 5

No intA + 1 6

No intA + 1 7

No intA + 1 8

No intA + 1 9

No intA + 1 10

Yes Exit

Page 7: Chapter 7 Loops Programming In Visual Basic.NET Prepared by Johnny Tsui, CIM@VTC

7

Example:intA = 0Do While intA < 10

intA = intA + 1Loop

<10? Action intA

Start 0

Do Until Yes intA + 1 1

Yes intA + 1 2

Yes intA + 1 3

Yes intA + 1 4

Yes intA + 1 5

Yes intA + 1 6

Yes intA + 1 7

Yes intA + 1 8

Yes intA + 1 9

Yes intA + 1 10

No Exit

Page 8: Chapter 7 Loops Programming In Visual Basic.NET Prepared by Johnny Tsui, CIM@VTC

8

Do's - When Evaluated Bottom Evaluation, will always run at least

one timeDo … Loop WhileDo … Loop Until

Page 9: Chapter 7 Loops Programming In Visual Basic.NET Prepared by Johnny Tsui, CIM@VTC

9

Example:intA = 0Do

intA = intA + 1Loop Until intA = 10

Action intA =10 ?

Start 0

Loop Until intA + 1 1 No

intA + 1 2 No

intA + 1 3 No

intA + 1 4 No

intA + 1 5 No

intA + 1 6 No

intA + 1 7 No

intA + 1 8 No

intA + 1 9 No

intA + 1 10 Yes

Exit

Page 10: Chapter 7 Loops Programming In Visual Basic.NET Prepared by Johnny Tsui, CIM@VTC

10

Example:intA = 0Do

intA = intA + 1Loop While intA < 10

Action intA <10 ?

Start 0

Loop Until intA + 1 1 No

intA + 1 2 No

intA + 1 3 No

intA + 1 4 No

intA + 1 5 No

intA + 1 6 No

intA + 1 7 No

intA + 1 8 No

intA + 1 9 No

intA + 1 10 Yes

Exit

Page 11: Chapter 7 Loops Programming In Visual Basic.NET Prepared by Johnny Tsui, CIM@VTC

11

Do Loops / For Next Loops:Deciding Which To Use

Is thenumber ofrepetitions

known?

Is theexpression

initiallytrue?

Mustthe loopexecuteonce?

Top Eval Do Loop

Bottom Eval Do LoopFor Next Loop

Do Until Loop Do While Loop

NO

YES

NO

NO YES

YES

Page 12: Chapter 7 Loops Programming In Visual Basic.NET Prepared by Johnny Tsui, CIM@VTC

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

Page 13: Chapter 7 Loops Programming In Visual Basic.NET Prepared by Johnny Tsui, CIM@VTC

13

For Next Loop General Form

For LoopIndex = InitialValue To TestValue [Step Increment]

Statements Next [LoopIndex]

Page 14: Chapter 7 Loops Programming In Visual Basic.NET Prepared by Johnny Tsui, CIM@VTC

14

Example:intA = 0intB = 0For intA = 1 To 10

intB = intB + 1Next

intA intA>10 ? intB

Start 0 0

For 1 No 1

2 No 2

3 No 3

4 No 4

5 No 5

6 No 6

7 No 7

8 No 8

9 No 9

10 No 10

Exit 11 Yes

Page 15: Chapter 7 Loops Programming In Visual Basic.NET Prepared by Johnny Tsui, CIM@VTC

15

Example:intA = 0intB = 0For intA = 1 To 10 Step 2

intB = intB + 1Next

intA intA>10 ? intB

Start 0 0

For 1 No 1

3 No 2

5 No 3

7 No 4

9 No 5

Exit 11 Yes

Page 16: Chapter 7 Loops Programming In Visual Basic.NET Prepared by Johnny Tsui, CIM@VTC

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

Page 17: Chapter 7 Loops Programming In Visual Basic.NET Prepared by Johnny Tsui, CIM@VTC

17

Example:intA = 0intB = 0For intA = 1 To 10

intB = intB + 1If intB = 3 Exit For

End IfNext

intA intA>10 ? intB intB=3 ?

Start 0 0

For 1 No 1 No

2 No 2 No

3 No 3 Yes

Exit

Page 18: Chapter 7 Loops Programming In Visual Basic.NET Prepared by Johnny Tsui, CIM@VTC

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.5Next

(b)For 4 = 1 to 10 Step 2Next

(c) For intIndex = 100 to 0 Step -25Next

(d)For intIndex = 0 to -10 Step -1Next

(e)For intIndex = 10 to 1Next

Name: Class:

Page 19: Chapter 7 Loops Programming In Visual Basic.NET Prepared by Johnny Tsui, CIM@VTC

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: