Title: Private Sub bntRedDemo_Click(
1 Private Sub bntRedDemo_Click(.
Dim intTheOnes As Integer
For intTheOnes 0 To 9
bntRedDemo.Text intTheOnes.ToString
bntRedDemo.Refresh()
Sleep(100) Next intTheOnes
End Sub
1
2
9
2 Private Sub bntRedDemo_Click(.
Dim intTheOnes As Integer
For intTheOnes 0 To 9
bntRedDemo.Text 0 intTheOnes.ToString
bntRedDemo.Refresh()
Sleep(100) Next intTheOnes
End Sub
01
02
09
3 Private Sub bntRedDemo_Click(.
Dim intTheTens As Integer
Dim intTheOnes As Integer
For intTheTens 0 To 9 For
intTheOnes 0 To 9
bntRedDemo.Text intTheTens.ToString
intTheOnes.ToString
bntRedDemo.Refresh() Sleep(100)
Next intTheOnes Next
intTheTens End Sub
01
02
10
99
Our first look at a nested loop
4 Dim intLoopCount As Integer For
intLoopCount 1 To 7 Select Case
intLoopCount Case 1
bntRedDemo.Text "moanday"
Case 2 bntRedDemo.Text
"tearsday" Case 3
bntRedDemo.Text "wailsday"
Case 4 bntRedDemo.Text
"thumpsday" Case 5
bntRedDemo.Text "frightday"
Case 6 bntRedDemo.Text
"shatterday" Case 7
bntRedDemo.Text "Sunday" End
Select Next intLoopCount
moanday
tearsday
shatterday
All moanday, tearsday, wailsday, thumpsday,
frightday, shatterday till the fear of the Law.
James Joyce, Finnegans Wake
5Dim intLoopCount, intOuterLoop As Integer For
intOuterLoop 1 To 4 For intLoopCount
1 To 7 Select Case intLoopCount
Case 1
bntRedDemo.Text "moanday" Case
2 bntRedDemo.Text
"tearsday" Case 3
bntRedDemo.Text "wailsday"
Case 4 bntRedDemo.Text
"thumpsday" Case 5
bntRedDemo.Text "frightday"
Case 6 bntRedDemo.Text
"shatterday" Case 7
bntRedDemo.Text "Sunday" End
Select Next intLoopCount Next
intOuterLoop
moanday
tearsday
shatterday
moanday
tearsday
6 Dim intLoopCount, intOuterLoop As Integer
Dim strWeekText As String For
intOuterLoop 1 To 4 strWeekText
"Week " (intOuterLoop).ToString
For intLoopCount 1 To 7 Select
Case intLoopCount Case 1
bntRedDemo.Text strWeekText
" moanday" Case 2
bntRedDemo.Text strWeekText "
tearsday" Case 3
bntRedDemo.Text strWeekText "
wailsday" Case 4
bntRedDemo.Text strWeekText "
thumpsday" Case 5
bntRedDemo.Text strWeekText "
frightday" Case 6
bntRedDemo.Text strWeekText "
shatterday" Case 7
bntRedDemo.Text strWeekText "
Sunday" End Select
Next intLoopCount Next intOuterLoop
7 Dim intLoopCount As Integer For
intLoopCount 1 To 7 Select Case
intLoopCount Case 1
bntRedDemo.Text "moanday"
Case 2 bntRedDemo.Text
"tearsday" Case 3
bntRedDemo.Text "wailsday"
Case 4 bntRedDemo.Text
"thumpsday" Case 5
bntRedDemo.Text "frightday"
Case 6 bntRedDemo.Text
"shatterday" Case 7
bntRedDemo.Text "Sunday" End
Select Next intLoopCount
moanday
tearsday
shatterday
Recall this example from a few slides ago.. We
could make it more readable
8Const MONDAY 1 Const SUNDAY 7 Dim
intDayOfWeek As Integer For intDayOfWeek
MONDAY To SUNDAY Select Case
intDayOfWeek Case 1
bntRedDemo.Text "moanday"
Case 2 bntRedDemo.Text
"tearsday" Case 3
bntRedDemo.Text "wailsday"
Case 4 bntRedDemo.Text
"thumpsday" Case 5
bntRedDemo.Text "frightday"
Case 6 bntRedDemo.Text
"shatterday" Case 7
bntRedDemo.Text "Sunday" End
Select Next intDayOfWeek
This code is logically identical, but reads
better
9The Do While loop is often used when the
repeating process is not controlled by counting
with a fixed-step amount, as in a For loop.
Do While (Condition) Program Statement(s) Loop
Instead, use the Do While construct when the loop
will continue while a certain condition evaluates
to True.
10Suppose we keep adding the integers 1 2 to get
3, then plus 3 to get 6, then plus 4 to get 10
etc. How many times can we do this before we
reach 100?
11"At " intLoopCount.ToString " and the total
is " intTotalSum.ToString
Dim intLoopCount, intTotalSum As
Integer intTotalSum 0
intLoopCount 0 Do While (intTotalSum
lt 100) intLoopCount intLoopCount
1 intTotalSum intTotalSum
intLoopCount bntRedDemo.Text "At "
intLoopCount.ToString " and the total is "
intTotalSum.ToString Loop
12In the previous examples we have seen lots of
code like this intLoopCount
intLoopCount 1 This is used so much, it has a
special name (increment) and a special shortcut
syntax
Operation Long Way of Writing the Statement Short Way of Writing the Statement
Addition intVar intVar 1 intVar 1
Subtraction intVar intVar 1 intVar - 1
Division intVar intVar / 2 intVar / 2
Multiplication intVar intVar 2 intVar 2
String concatenation strVar strVar New Text strVar New Text