Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter81 For....Next Loops n For i = m To n statement (s) Next i n For statement designates a numeric variable, called control variable. n It is initialized.

Similar presentations


Presentation on theme: "Chapter81 For....Next Loops n For i = m To n statement (s) Next i n For statement designates a numeric variable, called control variable. n It is initialized."— Presentation transcript:

1 Chapter81 For....Next Loops n For i = m To n statement (s) Next i n For statement designates a numeric variable, called control variable. n It is initialized and then automatically changes after each execution of loop. The Next statement increments the control variable n If m<=n, then i is assigned the values m, m+1,...., n in order the body is executed once for each of these values. n If m>n, then execution continues with the statement after the For...Next loop.

2 Chapter82 For....Next Loop n For example: p278 ex1 Th following program displays a table showing the population each year until 2000 Private Sub cmdDisplay_Click() Dim pop As Single, yr As Integer 'Display population from 1998 to 2002 picTable.Cls pop = 300000 For yr = 1998 To 2002 picTable.Print yr, FormatNumber(pop, 0) pop = pop + 0.03 * pop Next yr End Sub

3 Chapter83 For....Next Loop n Flowchart of a For...Next loop

4 Chapter84 For....Next Loop (Nested Loop) n There are three type of nested loops For jFor iFor i For k For j For j For k Next j Next k For j For k Next k Next k Next k For k Next j Next j For m Next i Next m Next k Next j Next i

5 Chapter85 For....Next Loop (Nested Loop) n Example p.281 ex4 A multiplication table for integer 1 to 4 Private Sub cmdDisplay_Click() Dim j As Integer, k As Integer picTable.Cls For j = 1 To 4 For k = 1 To 4 picTable.Print j; "x"; k; "="; j * k, Next k picTable.Print Next j End Sub

6 Chapter86 Arrays n An Array variable is a collection of simple variables of the same type n For example –Private Sub cmdButton_Click() Dim student1 As String, score1 As Single Dim student2 As String, score2 As Single Dim student3 As String, score3 As Single n It would be nice if we could just write For i = 10 To 30 Input #1, studenti, scorei Next i n However, this will not work as VB will treat studenti and scorei as two variables.

7 Chapter87 Arrays n VB provides a data structure called an array. n The variables names will be similar to those in the Input # statement. They will be student(1), student(2), student(3),..........student(30) score(1),score(2),score(3)................score(30). n This collections of variables as the array variables student( ) and score( ). n The numbers inside the parenthese of the individual variables are called subscripts. n Each individual variable is called a subscripted variable or element. score ()score(2) score(3)..............score(30) n score()

8 Chapter88 Arrays n The statement of the array will be –Dim student(1 To 30) As String –Dim score (1 To 30) As Integer n For example, the following program creates a string array consisting of the names of the first fie World Series winners. teamName(1) teamName(2) teamName(3) teamName(4) n teamName() Red Sox Giants White Sox Cubs

9 Chapter89 Arrays 'Create array for five strings Dim teamName(5) As String Private Sub cmdWhoWon_Click() Dim n As Integer 'Fill array with World Series Winners teamName(1) = "Red Sox" teamName(2) = "Giants" teamName(3) = "White Sox" teamName(4) = "Cubs" teamName(5) = "Cubs" 'Access array of five strings n = Val(txtNumber.Text) picWinner.Cls picWinner.Print "The "; teamName(n); " won World Series number"; n End Sub

10 Chapter810 Arrays n When the statement Dim arrayName(1 To n) As varType is placed inside an event procedure or general procedure, then space for n subscripted variables is set aside in memory each time the procedure is invoked and released when procedure is exited. n For example: Private Sub cmdShow_Click() Dim total As Integer, student As Integer, average As Single Dim nom(1 To 8) As String, score(1 To 8) As Integer Open App.Path & "\SCORES.TXT" For Input As #1

11 Chapter811 Arrays n In some applications, some arrays whose size was not know before the program was run. n ReDim statements allowed us to create an unknown size arrays n For example: Private Sub cmdShow_Click() Dim numStudents As Integer, nTemp As String, sTemp As Integer Dim student As Integer, total As Integer, average As Single numStudents = 0 Open App.Path & "\SCORES.TXT" For Input As #1 Do While Not EOF(1) Input #1, nTemp, sTemp numStudents = numStudents + 1 Loop Close #1 ReDim nom(1 To numStudents) As String, score(1 To numStudents) As Integer

12 Chapter812 Arrays n Some applications required form level arrays whose size is not known in advance n Dim statements cannot use variables or expressions to specify the subscript range. n The solution would be Dim arrayName() As var Type n For example, Dim team() As string Dim teamName() As string

13 Chapter813 Arrays n An entire array can be passed to another procedure. n For example: Private Sub cmdDisplay_Click() 'Pass array to a Sub procedure and a Function procedure Dim score(1 To 10) As Integer Call FillArray(score()) picAverage.Cls picAverage.Print "The average score is"; Sum(score()) / 10 End Sub Private Sub FillArray(s() As Integer) End Sub

14 Chapter814 Control Arrays n VB provides a means of constructing arrays of text boxes, labels, command button n Because text boxes, labels, and command buttons are referred to generically in VB as controls, arrays of this objects are called control arrays. n For example: txtBox(0).Text = value txtBox(1).Text = value n Explain p.339 example 7.3-1 n Explain p.341 example 7.3.2

15 Chapter815 Exercise n P.282 Exercise 6.3 No. 13,15,17,23 n P.315 Exercise 7.1 No. 1, 3, 5, 17 n P.336 Exercise 7.2 No. 13 n P.349 Exercise 7.3 No. 35


Download ppt "Chapter81 For....Next Loops n For i = m To n statement (s) Next i n For statement designates a numeric variable, called control variable. n It is initialized."

Similar presentations


Ads by Google