|
-
July 6th, 2001, 04:09 AM
#1
VB For, Next question
somethin“s troubleing me, heres an example:
if i run this code:
Dim MyInteger as Integer
Dim I as Integer
MyInteger = 0
for I = 1 to MyInteger
(other code here)
Next I
Does the code inside the For,Next Executes at least once??
And,, WHAT IN GODS NAME is the value of "I" after i run the code?
lets say i after the For,Next write:
If I > MyInteger Then (other code here)
Does "other code here" executes??????
is the condition met?? <IMG SRC="smilies/confused.gif" border="0">
-
July 6th, 2001, 11:28 AM
#2
Dim i as Integer
For i = 1 to expression
...your code...
Next
Expression is a number. The number does not need to be declared and can always change at runtime.
Example:
I will use a recordset(rs) that will process instructions when ever a record is present. The instructions are to display a message box for every record in the rs.RecordCount method.
For i = 1 to rs.RecordCount 'RecordCount will give a total number of records. lets say 5
MsgBox "Looping"
Next
The code above will display a message box "Looping" 5 times because there are 5 records.
Put your If statements into the For..Next loop.
Example:
For i = 1 to rs.RecordCount
If rs.RecordCount = 3 Then
MsgBox "Your on the third record"
End if
Next
Once the loop reaches the third record and message box will display "Your on the third record".
-
July 7th, 2001, 10:24 PM
#3
Thanks Opiate for your answer, but actually what i was wondering is, since the value of "MyInteger" (on my example)has a value of 0 which is is smaller that the original "counter" (For) value of 1
will the code inside the For, Next
statement be executed at all?
Any way i figure it out now, the For,Next statement wil NOT be executed since the value of "MyInteger" is smaller (0), and the final value of "I" is 1.
Thanks anyway <IMG SRC="smilies/smile.gif" border="0">
-
July 9th, 2001, 04:29 PM
#4
Registered User
Sounds like you solved your own problem. You are correct. If the initial test on the loop, is 1 greater than zero, is false, then the code in the loop will not execute. In this case the loop would be bypassed altogether. Now if you are wanting to loop from one to zero, by decrementing the iterator(i), then after the for statement add "step -1". For example...
Dim MyInteger as Integer
MyInteger=0
For i=1 to MyInteger Step -1
code to be executed
Next i
This would start the loop at one and count down to zero.
Hope this helps. <IMG SRC="smilies/biggrin.gif" border="0">
A bored admin is a very dangerous person...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|
Bookmarks