Base64 EncodingFree VB.NET Resources From Microsoft

The Continue Statement

June 4th, 2007

Continue keeps things runningBefore 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.

Share This Article: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • StumbleUpon
  • Technorati
  • DotNetKicks
  • DZone

Entry Filed under: VB.NET Tutorials


Rate This Article:

Not That GoodCould Be BetterOKGoodGreat (2 votes, average: 4 out of 5)
Loading ... Loading ...

Leave a Comment

Required

Required, hidden

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


Visit Me At My New Site, Programming In C#

Most Popular Articles

Highest Rated Articles

Categories

Most Recent Articles

Feeds

 Subscribe in a reader

To subscribe by e-mail
Enter your address here

Delivered by FeedBurner

VB Opportunities

Archives