Transcript Chapter 07

Chapter 7
Loops
Programming In
Visual Basic.NET
Prepared by Johnny Tsui, CIM@VTC
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
2
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
3
Do Loop General Form
Do {While |Until} condition
Statements to execute
Loop
Top of Loop
Condition,
Pretest
OR
Do
Statements to execute
Loop {While | Until} condition
Bottom of Loop
Condition,
Posttest
4
Do's - When Evaluated

Top Evaluation, not guaranteed to run
once since evaluated BEFORE running
Do While … Loop
Do Until … Loop
5

Example:
intA = 0
Do Until intA = 10
intA = intA + 1
Loop
=10?
Action
Start
Do Until
intA
0
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
6

Example:
intA = 0
Do While intA < 10
intA = intA + 1
Loop
<10?
Action
Start
Do Until
intA
0
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
7
Do's - When Evaluated

Bottom Evaluation, will always run at least
one time
Do … Loop While
Do … Loop Until
8

Example:
intA = 0
Do
intA = intA + 1
Loop Until intA = 10
Action
Start
Loop Until
intA
=10 ?
0
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
9

Example:
intA = 0
Do
intA = intA + 1
Loop While intA < 10
Action
Start
Loop Until
intA
<10 ?
0
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
10
Do Loops / For Next Loops:
Deciding Which To Use
Is the
number of
repetitions
known?
NO
YES
NO
Top Eval Do Loop
YES
For Next Loop
Do Until Loop
Must
the loop
execute
once?
Bottom Eval Do Loop
NO
Is the
expression
initially
true?
YES
Do While Loop
11
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

12
For Next Loop General Form
For LoopIndex = InitialValue To TestValue
[Step Increment]
Statements
Next [LoopIndex]
13

Example:
intA = 0
intB = 0
For intA = 1 To 10
intB = intB + 1
Next
intA
intA>10 ? intB
Start
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
11
Yes
Exit
0
14

Example:
intA = 0
intB = 0
For intA = 1 To 10 Step 2
intB = intB + 1
Next
intA
intA>10 ? intB
Start
0
For
1
No
1
3
No
2
5
No
3
7
No
4
9
No
5
11
Yes
Exit
0
15
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

16

Example:
intA = 0
intB = 0
For intA = 1 To 10
intB = intB + 1
If intB = 3
Exit For
End If
Next
intA
intA>10 ? intB
intB=3 ?
Start
0
0
For
1
No
1
No
2
No
2
No
3
No
3
Yes
Exit
17
Name:
Class:
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)
(b)
(c)
(d)
(e)
For decIndex = 3.5 to 6.0, Step 0.5
Next
For 4 = 1 to 10 Step 2
Next
For intIndex = 100 to 0 Step -25
Next
For intIndex = 0 to -10 Step -1
Next
For intIndex = 10 to 1
Next
18
Name:
Class:
Exercises

How many times will the body of the
loop be executed for each of these
examples?
(a)
(b)
(c)
(d)
(e)
For
For
For
For
For
intIndex = 1 to 3
intIndex = 2 to 11 Step 3
intIndex = 10 to -1 Step -1
decIndex = 3.0 to 6.0 Step 0.5
intIndex = 5 to 1
19