Page 150 - Open Soource Technologies 304.indd
P. 150
Event Driven Programming
Object Property Setting
frmPopulation Text POPULATION GROWTH
btnDisplay lstTable Text Display Population
Private Sub btnDisplay_Click(...) Handles btnDisplay.Click
‘Display population from 2006 to 2010
Dim pop As Double = 300000, yr As Integer
Dim fmtStr As String = “{0,4}{1,12:N0}”
For yr = 2006 To 2010
lstTable.Items.Add(String.Format(fmtStr, yr, pop))
pop += 0.03 * pop
Next
End Sub
[Run, and click the button.]
The initial and terminating values can be literals, variables, or expressions. For instance, the For
statement in the preceding program can be replaced by
Dim firstYr As Integer = 2006
Dim lastYr As Integer = 2010
For yr = firstYr To lastYr
In Example 15, the control variable was increased by 1 after each pass through the loop. A
variation of the For statement allows any number to be used as the increment. The statement
For i = m To n Step s
instructs the Next statement to add s to the control variable instead of 1. The numbers m, n, and
s do not have to be whole numbers. The number s is called the step value of the loop. Note:
If the control variable will assume values that are not whole numbers, then the variable must
be of type Double.
2: The following program displays the values of the index of a For... Next loop
for terminating and step values input by the user:
144 LOVELY PROFESSIONAL UNIVERSITY