Reputation: 21
Can anyone tell the value of counter at each iteration?
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As Integer
For x = 1 To 10 Step (x)
x = x + 1
MsgBox(x)
Next
End Sub
End Class
Upvotes: 0
Views: 64
Reputation: 716
The iterations will be:
x=2
x=4
x=6
x=8
x=10
But how?
For the first time when you execute a loop the value will be x=1.
Later inside the loop the value of x becomes x=x+1 ie x=2.
Now this value is used as step so your code takes steps of size 2.
In VB once the size of a step is calculated it is not recalculated. For example here in the second iteration the value becomes x=4 that doesn't mean it will now take a step size 4. It will use previously calculated value ie 2.
Upvotes: 3
Reputation: 182
2-11 - Step(x) is evaluated when first called. (I'm right, yes - do I get a cookie?)
Upvotes: 1