Title: Public Class Form1
1(No Transcript)
2Public Class Form1 Inherits
System.Windows.Forms.Form Private Sub
bntRedDemo_Click(. Sleep(5000)
bntRedDemo.Text "You waited 5 seconds for
this?" End Sub Declare Sub Sleep
Lib "kernel32" (ByVal dwMilliseconds As
Long) End Class
Windows From Designer generated code
After 5 Seconds
3Sleep(5000) Pause for 5 seconds Sleep(1000)
Pause for 1 second Sleep(500) Pause for
1/2 second
4The For loop is very versatile with lots of
options. The following is the syntax
For LoopCounter InitialValue To
TerminatingValue Program Statement(s) Next
LoopCounter
The sequence for the correct evaluation of a For
loop is as follows 1 The LoopCounter is set to
the value InitialValue. 2 The LoopCounter is
compared to the TerminatingValue. If the
LoopCounter is greater than the TerminatingValue,
go to step 6. Otherwise, continue with the
execution of the loop. 3 The program statements
contained in the body of the loop are executed. 4
The value LoopCounter is incremented. 5 Go to
step 2. 6 Exit the loop.
5 Private Sub bntRedDemo_Click(.
Dim intCounter As Integer intCounter
0 For intCounter 1 To 5
bntRedDemo.Text (intCounter).ToString
bntRedDemo.Refresh() Sleep(1000)
Next intCounter End Sub
1
2
5
6 Private Sub bntRedDemo_Click(.
Dim intCounter As Integer intCounter
0 For intCounter 1 To 47
bntRedDemo.Text (intCounter).ToString
bntRedDemo.Refresh() Sleep(1000)
Next intCounter End Sub
1
2
47
7 Private Sub bntRedDemo_Click(.
Dim intCounter As Integer intCounter
0 For intCounter 11 To 47
bntRedDemo.Text (intCounter).ToString
bntRedDemo.Refresh() Sleep(1000)
Next intCounter End Sub
11
12
47
8 Private Sub bntRedDemo_Click(.
Dim intCounter As Integer intCounter
0 For intCounter 11 To 47
bntRedDemo.Text (intCounter).ToString
bntRedDemo.Refresh() Sleep(1000)
Next intCounter End Sub
From now on I will just show the loop body for
clarity. As always, we recall that the rest of
the code is really there
9 For intCounter 1 To 10 Step 2
bntRedDemo.Text (intCounter).ToString
bntRedDemo.Refresh() Sleep(1000)
Next intCounter
1
3
We can use the Step option to count in increments
other that 1 Lets show all the odd numbers from
1 to 10
9
10 For intCounter 10 To 1 Step -1
bntRedDemo.Text (intCounter).ToString
bntRedDemo.Refresh() Sleep(1000)
Next intCounter bntRedDemo.Text "We
have liftoff!"
10
9
1
We can use the Step option to count in increments
other that 1 We can use a negative step to count
backwards.
11Dim strS As String strS For intCounter
1 To 5 strS strS (intCounter).ToString
bntRedDemo.Text strS Next intCounter
But how do we do this?
12Case Study I
Lets us add up all the integers from 1 to 100 1
2 3 4 . 98 99 100
13 Dim intTotalCount As Integer intTotalCount
0 For intCounter 1 To 100
intTotalCount intTotalCount intCounter
Next intCounter bntRedDemo.Text
intTotalCount.ToString
14Case Study II
Lets us multiply up all the integers from 1 to
10 1 2 3 4 5 6 7 8 9 10 Also
called 10 factorial, denoted as 10!
15 Dim intTotal As Integer intTotal 1
For intCounter 1 To 10 intTotal
intTotal intCounter Next intCounter
bntRedDemo.Text intTotal.ToString
16Case Study III
Lets us multiply up all the integers from 1 to
100 1 2 3 4 5 98 99 100
Stop We can not do this. 100! Is much larger
than 2,147,483,647, in fact 100! Is much larger
than our long type (9,223,372,036,854,775,807)
17Case Study II
Lets us add up all the integers from Low to High,
where Low and High are not known in advance Low
(Low 1) (Low 2) High
18 Dim intTotalCount, intLow, intHigh As Integer
intTotalCount 0 intLow assume some code
that gets the value intHigh assume some
code that gets the value For intCounter
intLow To intHigh intTotalCount
intTotalCount intCounter Next
intCounter bntRedDemo.Text
intTotalCount.ToString