Jump to content
Tuts 4 You
Sign in to follow this  
alaphate

Is this the best Continue in a loop of VB6

Recommended Posts

alaphate

There's no continue directive in VB6.

Is this code below is the best (shortest) solution?

  Dim i As Integer  For i = 1 To 5
If i = 3 Then
GoTo CNT
End If
Print i
CNT:
Next
Edited by alaphate (see edit history)

Share this post


Link to post
ghandi

Check out this page on MSDN:

http://msdn.microsoft.com/en-us/library/t2at9t47(VS.80).aspx

It explains the following snippet:

Sub exitStatementDemo()
Dim demoNum As Single
' Set up an infinite loop.
Do
For i As Integer = 1 To 10000000
demoNum = Int(Rnd() * 100)
Select Case demoNum
Case 7 : Exit For
Case 29 : Exit Do
Case 54 : Exit Sub
End Select
Next i
Loop
End Sub

Basically you can use "Exit For" to break out of a "For" loop. Im not sure how applicable this is to VB6, as i dont program in either VB6 or VB .NET.

Just installed VB6 to check it out and it had a problem with declaring i as an integer if i left it as part of the For declaration and it wasnt happy with such a large integer, but changing it to:

Sub exitStatementDemo()
Dim demoNum As Single
Dim i As Integer ' Set up an infinite loop.
Do
For i = 1 To 10000
demoNum = Int(Rnd() * 100)
Select Case demoNum
Case 7 : Exit For
Case 29 : Exit Do
Case 54 : Exit Sub
End Select
Next i
Loop
End Sub

Then it compiled fine...

HR,

Ghandi

Edited by ghandi (see edit history)

Share this post


Link to post
alaphate

Ghandi,

Thanks for the code.

However, Exit For just like c++ break

It break out from the FOR loop

In my sample code, It will print out: 1 2 4 5

and skip 3, so when i=3, it will continue the loop

Share this post


Link to post
ghandi
  Dim i As Integer  For i = 1 To 5
If not i = 3 Then
Print i
End If Next

It could be done like this also. This just means that the default case isnt 3, so it will execute the code inside the "If not" statement and only jumps over the code when i=3.

HR,

Ghandi

Edited by ghandi (see edit history)

Share this post


Link to post
alaphate

Ghandi,

Your code is better than mine :)

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
×
×
  • Create New...