Presentation is loading. Please wait.

Presentation is loading. Please wait.

Private Sub bntRedDemo_Click(…. Dim intTheOnes As Integer For intTheOnes = 0 To 9 bntRedDemo.Text = intTheOnes.ToString bntRedDemo.Refresh() Sleep(100)

Similar presentations


Presentation on theme: "Private Sub bntRedDemo_Click(…. Dim intTheOnes As Integer For intTheOnes = 0 To 9 bntRedDemo.Text = intTheOnes.ToString bntRedDemo.Refresh() Sleep(100)"— Presentation transcript:

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 99 10 … … 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 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

5 Dim 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 Dim 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 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

8 Const 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 Const 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…

9 Do While (Condition) Program Statement(s) Loop Do While (Condition) Program Statement(s) Loop The 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. Instead, use the Do While construct when the loop will continue while a certain condition evaluates to True.

10 Suppose 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 Dim intLoopCount, intTotalSum As Integer intTotalSum = 0 intLoopCount = 0 Do While (intTotalSum <= 100) intLoopCount = intLoopCount + 1 intTotalSum = intTotalSum + intLoopCount bntRedDemo.Text = "At " & intLoopCount.ToString & " and the total is " & intTotalSum.ToString Loop … …" At " & intLoopCount.ToString & " and the total is " & intTotalSum.ToString

12 OperationLong Way of Writing the StatementShort Way of Writing the Statement Addition intVar = intVar + 1intVar += 1 Subtraction intVar = intVar – 1intVar -= 1 Division intVar = intVar / 2intVar /= 2 Multiplication intVar = intVar * 2intVar *= 2 String concatenation strVar = strVar & “New Text”strVar &= “New Text” In 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…


Download ppt "Private Sub bntRedDemo_Click(…. Dim intTheOnes As Integer For intTheOnes = 0 To 9 bntRedDemo.Text = intTheOnes.ToString bntRedDemo.Refresh() Sleep(100)"

Similar presentations


Ads by Google