Jump to content
Tuts 4 You

Is this the best Continue in a loop of VB6


alaphate

Recommended Posts

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
Link to comment

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
Link to comment

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

Link to comment
  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
Link to comment

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
×
×
  • Create New...