Section 2.10. Skip to the Next Iteration of a Loop


2.10. Skip to the Next Iteration of a Loop

The Visual Basic language provides a handful of common flow control statements, which let you direct the execution of your code. For example, you can use Return to step out of a function, or Exit to back out of a loop. However, before VB 2005, there wasn't any way to skip to the next iteration of a loop.


Note: VB's new Continue keyword gives you a quick way to step out of a tangled block of code in a loop and head straight into the next iteration.

2.10.1. How do I do that?

The Continue statement is one of those language details that seems like a minor frill at first, but quickly proves itself to be a major convenience. The Continue statement exists in three versions: Continue For, Continue Do, and Continue While, each of which is used with a different type of loop (For ... Next, Do ... Loop, or While ... End While).

To see how the Continue statement works consider the following code:

For i = 1 to 1000     If i Mod 5 = 0 Then         ' (Task A code.)         Continue For     End If     ' (Task B code.) Next

This code loops 1,000 times, incrementing a counter i. Whenever i is divisible by five, the task A code executes. Then, the Continue For statement is executed, the counter is incremented, and execution resumes at the beginning of the loop, skipping the code in task B.

In this example, the continue statement isn't really required, because you could rewrite the code easily enough as follows:

For i = 1 to 1000     If i Mod 5 = 0 Then         ' (Task A code.)     Else         ' (Task B code.)     End If Next

However, this isn't nearly as possible if you need to perform several different tests. To see the real benefit of the Continue statement, you need to consider a more complex (and realistic) example.

Example 2-5 demonstrates a loop that scans through an array of words. Each word is analyzed, and the program decides whether the word is made up of letters, numeric characters, or the space character. If the program matches one test (for example, the letter test), it needs to continue to the next word without performing the next test. To accomplish this without using the Continue statement, you need to use nested loops, an approach that creates awkward code.

Example 2-5. Analyzing a string without using the Continue statement
' Define a sentence. Dim Sentence As String = "The final number is 433."      ' Split the sentence into an array of words. Dim Delimiters( ) As Char = {" ", ".", ","} Dim Words( ) As String = Sentence.Split(Delimiters)      ' Examine each word. For Each Word As String In Words     ' Check if the word is blank.     If Word <> "" Then         Console.Write("'" + Word + "'" & vbTab & "= ")              ' Check if the word is made up of letters.         Dim AllLetters As Boolean = True         For Each Character As Char In Word             If Not Char.IsLetter(Character) Then                 AllLetters = False             End If         Next         If AllLetters Then             Console.WriteLine("word")         Else             ' If the word isn't made up of letters,             ' check if the word is made up of numbers.             Dim AllNumbers As Boolean = True             For Each Character As Char In Word                If Not Char.IsDigit(Character) Then                    AllNumbers = False                End If             Next             If AllNumbers Then                 Console.WriteLine("number")             Else                 ' If the word isn't made up of letters or numbers,                 ' assume it's something else.                 Console.WriteLine("mixed")             End If         End If     End If Next

Now, consider the rewritten version shown in Example 2-6 that uses the Continue statement to clarify what's going on.

Example 2-6. Analyzing a string using the Continue statement
' Examine each word. For Each Word As String In Words     ' Check if the word is blank.     If Word = "" Then Continue For     Console.Write("'" + Word + "'" & vbTab & "= ")          ' Check if the word is made up of letters.     Dim AllLetters As Boolean = True     For Each Character As Char In Word         If Not Char.IsLetter(Character) Then             AllLetters = False         End If     Next     If AllLetters Then         Console.WriteLine("word")         Continue For     End If          ' If the word isn't made up of letters,     ' check if the word is made up of numbers.     Dim AllNumbers As Boolean = True     For Each Character As Char In Word         If Not Char.IsDigit(Character) Then             AllNumbers = False         End If     Next     If AllNumbers Then         Console.WriteLine("number")         Continue For     End If          ' If the word isn't made up of letters or numbers,     ' assume it's something else.     Console.WriteLine("mixed") Next

2.10.2. What about...

...using Continue in a nested loop? It's possible. If you nest a For loop inside a Do loop, you can use Continue For to skip to the next iteration of the inner loop, or Continue Do to skip to the next iteration of the outer loop. This technique also works in reverse (with a Do loop inside a For loop), but it doesn't work if you nest a loop inside another loop of the same type. In this case, there's no unambiguous way to refer to the outer loop, and so your Continue statement always refers to the inner loop.

2.10.3. Where can I learn more?

For the language lowdown on Continue, refer to the index entry "continue statement" in the MSDN Help.



Visual Basic 2005(c) A Developer's Notebook
Visual Basic 2005: A Developers Notebook
ISBN: 0596007264
EAN: 2147483647
Year: 2006
Pages: 123

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net