tags:

views:

1877

answers:

4

Is there a VB6 equivalent to the C/C++ 'continue' keyword?

In C/C++, the command 'continue' starts the next iteration of the loop.

Of course, other equivalents exist. I could put the remaining code of the loop in an if-statement. Alternatively, I could use a goto. (Ugh!)

+1  A: 

I´m an idiot :P thanks MarkJ

   For index As Integer = 1 To 10
        If  index=9 Then
            Continue For
        End If
   'some cool code'
    Next

No sorry only for .net. I think you have to use goto, I know it looks ‘cleaner’ to use a continue but there is nothing wrong with going the if route.

wrong.

 Continue:
     For index As Integer = 1 To 10
                If index=9 Then
                  GoTo Continue
                End If
    'some cool code'
     Next

corrected(?)

     For index = 1 To 10
      If index=9 Then
       GoTo Continue 
      End If    
    'some cool code'
Continue:
     Next

hate vb

vaquito
Your label is in the wrong place. That would restart the loop from 1 every time - infinite loop. In my opinion it's best to put it just before the Next. In fact you've got a serious case of air code ;) You've included the "As Integer" in the VB6 For statement, which is not valid VB6, and you've used i and index for the variable name in both your VB.NET and your VB6.
MarkJ
+3  A: 

Sadly there is no Continue if VB6 - this was new in VB 2005 I believe.

I wouldn't always be afraid of goto statements - that's effectively what the Continue is, but without the need for a labelled line after the loop. As long as your goto statements don't jump very far, they will always be readable, and it is probably the most elegant solution to this problem.

Nesting another if/then/else inside a for loop is actually HARDER to read and maintain later than a nice simple goto (with a comment on the goto line saying something like "' read as Continue For").

Good luck!

Mike
+4  A: 

There is no equivalent in VB6, but later versions of VB do introduce this keyword. This article has a more in-depth explanation: http://vbnotebookfor.net/2007/06/04/the-continue-statement/

Perhaps you can restructure your code to either add an if statement or have the loop just call a function that you can return from.

Justin Ethier
+1  A: 

VB6 has no continue statement for loops. You have to emulate it using goto, if, or another loop.

//VB.net
do
    if condition then continue do
    ...
loop
//VB6 equivalent (goto)
do
    if condition then goto continue_do
    ...
continue_do:
loop
//VB6 equivalent (if)
do
    if not condition then
        ...
    endif
loop
//VB6 equivalent (fake inner loop)
do
    while true
        if condition then exit while
        ...
        exit while
    wend
loop
Strilanc