The Continue Statement
June 4th, 2007
Before VS 2005 there was a missing piece of the puzzle in VB when it came to flow control in For…Next and Do loops, there was no good way to continue processing in a loop. This led to some programmers using crazy coding techniques, like huge If…ElseIf…Else blocks, or even the dreaded GoTo to implement it. Now, we have the Continue statement that allows us to skip to the next iteration of a loop with minimal code or heavy indenting. Here’s how we can put it to work.
If you’ve ever seen an bit of VB6 code like this:
.
.
Do While SomeFlag
'20 lines of code here
If Cond1 = TestFunction() Then
'3 lines of code here
Else
'100 more lines of code here
End If
Loop
Or, even worse, this:
Do While SomeFlag
'20 lines of code here
If Cond1 = TestFunction() Then
'3 lines of code here
GoTo EndOfLoop
Else
'100 more lines of code here
End If
EndOfLoop:
Loop
The value of the Continue statement in terms of code readability and maintainability is probably already apparent to you.
Do While SomeFlag
'20 more lines of code here
If Cond1 = TestFunction() Then
'3 lines of code here
Continue Do
End If
'100 more lines of code here
Loop
By using it we get rid of a level of nested indenting, which can make reading the code more difficult, and we avoid the ambiguous and dangerous GoTo since Continue makes it clear where the next point of execution will be.
The Continue statement is modified by the type of looping structure it’s used in: For, Do, or While. This comes into play when you have nested looping code. For example:
Do While SomeFlag
'20 more lines of code here
If Cond1 = TestFunction() Then
For Counter As Integer = 0 To 9
'4 more lines of code here
If TestValue < 6 Then
'3 more lines of code here
If Not SomeFlag Then
Continue Do
Else
Continue For
End If
End If
'25 more lines of code here
If Not SomeFlag Then
Continue Do
End If
Next
End If
'100 more lines of code here
Loop
In this example, we want to continue the inner For…Next loop in some conditions and continue the outer Do loop in others. Continue makes this easy to do. But, what if you have two nested loops of the same type?
Do While SomeFlag
'20 more lines of code here
If Cond1 = TestFunction() Then
Do Until Counter = 10
'4 more lines of code here
If TestValue < 6 Then
Continue Do
End If
'25 more lines of code here
Loop
'15 more lines of code here
If Not SomeFlag Then
Continue Do
End If
End If
'100 more lines of code here
Loop
In this case, the inner loop takes precedence. In the example above the first Continue Do would move to the next iteration of the inner loop while the second one would work with the outer loop.
The thing to be careful about when using Continue is to be mindful of how you are structuring your code. Long routines may benefit from being broken up into 2 or more smaller chunks. Complex conditional situations may make Continue impractical. However, if used wisely, Continue can make your program’s flow more readable and maintainable.
Entry Filed under: VB.NET Tutorials
Rate This Article:











Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed