180 likes | 196 Views
Public 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.
E N D
Public 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
Sleep(5000) ‘Pause for 5 seconds Sleep(1000) ‘Pause for 1 second Sleep(500) ‘Pause for 1/2 second
The For loop is very versatile with lots of options. The following is the syntax: ForLoopCounter = InitialValue ToTerminatingValue Program Statement(s) NextLoopCounter 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.
Private Sub bntRedDemo_Click(…. DimintCounter As Integer intCounter = 0 ForintCounter = 1 To5 bntRedDemo.Text = (intCounter).ToString bntRedDemo.Refresh() Sleep(1000) NextintCounter End Sub 1 2 … 5
Private Sub bntRedDemo_Click(…. DimintCounter As Integer intCounter = 0 ForintCounter = 1 To47 bntRedDemo.Text = (intCounter).ToString bntRedDemo.Refresh() Sleep(1000) NextintCounter End Sub 1 2 … 47
Private Sub bntRedDemo_Click(…. DimintCounter As Integer intCounter = 0 ForintCounter = 11 To47 bntRedDemo.Text = (intCounter).ToString bntRedDemo.Refresh() Sleep(1000) NextintCounter End Sub 11 12 … 47
Private Sub bntRedDemo_Click(…. DimintCounter As Integer intCounter = 0 ForintCounter = 11 To47 bntRedDemo.Text = (intCounter).ToString bntRedDemo.Refresh() Sleep(1000) NextintCounter 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
ForintCounter = 1 To10 Step 2 bntRedDemo.Text = (intCounter).ToString bntRedDemo.Refresh() Sleep(1000) NextintCounter 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
ForintCounter = 10 To1 Step -1 bntRedDemo.Text = (intCounter).ToString bntRedDemo.Refresh() Sleep(1000) NextintCounter 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.
Dim strS As String strS = “” ForintCounter = 1 To5 strS = strS & (intCounter).ToString bntRedDemo.Text = strS NextintCounter But how do we do this?
Case Study I Lets us add up all the integers from 1 to 100 1 + 2 + 3 + 4 …. + 98 + 99 + 100
Dim intTotalCount As Integer intTotalCount = 0 For intCounter = 1 To 100 intTotalCount = intTotalCount + intCounter Next intCounter bntRedDemo.Text = intTotalCount.ToString
Case 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!
Dim intTotal As Integer intTotal = 1 For intCounter = 1 To 10 intTotal = intTotal * intCounter Next intCounter bntRedDemo.Text = intTotal.ToString
Case 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)
Case 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
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