Control Statements in VB.NET: Part 2

In this article I will explain your about different control statements in VB.NET.
  • 3143

As you learn some control statement in previous article here is part 2 of remaining controls statement.

Control statements give you additional means to control the processing within the applications you develop. This section explores the syntax and function of the do-while, foreach, goto, exit, continue, and return statements.

While

The while loop allows the user to repeat a section of code until a guard condition is met. Listing 5.27 presents a simple while loop designed to find out the number of digits in a given value.

Listing 5.27: While Example

        'find out the number of digits in a given number
        Dim i As Integer = 123
        Dim count As Integer = 0
        Dim n As Integer = i
         'while loop may execute zero times
        While i > 0
            System.Threading.Interlocked.Increment(count)
            i = i / 10
        End While
        Console.WriteLine("Number {0} contains {1} digits.", n, count)

For a given number i = 123, the loop will execute three times. Hence the value of the count is three at the end of the while loop.

This example has one logical flaw. If the value of i is 0, the output of the code will be "Number 0 contains 0 digits." Actually, the number 0 contains one digit. Because the condition of the while loop i > 0 is false from the beginning for the value i = 0, the while loop does not even execute one time and the count will be zero. Listing 5.28 presents a solution.

Listing 5.28: Do Example

        'find out the number of digits in a given number
        Dim i As Integer = 0
        Dim count As Integer = 0
        Dim n As Integer = i
        Do
            System.Threading.Interlocked.Increment(count)
            i = i / 10
        Loop While i > 0
        Console.WriteLine("Number {0} contains {1} digits.", n, count)

The do-while construct checks the condition at the end of the loop. Therefore, the do-while loop executes at least once even though the condition to be checked is false from the beginning.

ForEach

The For Each statement allows the iteration of processing over the elements in arrays and collections. Listing 5.31 contains a simple example.

Listing 5.31: For Each Example 1

        'foreach loop
        Dim a As String() = {"Chirag", "Bhargav", "Tejas"}
        For Each b As String In a
            Console.WriteLine(b)

        Next

Listing 5.32 presents a slightly more complex version of the foreach loop.

Listing 5.32: ForEach Example 2

        Dim intNumbers As Int16() = {4, 5, 6, 1, 2, 3, -2, -1, 0}
        For Each i As Int16 In intNumbers
            System.Console.WriteLine(i)
        Next

Each iteration queries the collection for a new value for i. As long as the collection intNumbers returns a value, the value is put into the variable i and the loop will continue. When the collection is fully traversed, the loop will terminate.

GoTo

You can use the goto statement to jump to a specific segment of code, as shown in Listing 5.33. You can also use goto for jumping to switch cases and default labels inside switch blocks. You should avoid the overuse of goto because code becomes difficult to read and maintain if you have many goto jumps within your code.

Listing 5.33: GoTo Example

        label1:

         '...
        If x = 0 Then
            GoTo label1
        End If
Exit

The exit statement, used within for, while, and do-while blocks, causes processing to exit the innermost loop immediately. When a break statement is used, the code jumps to the next line following the loop block, as you'll see in Listing 5.34.

Listing 5.34: Exit Example

While True
            '...
            If x = 0 Then
                Exit While
                '...
            End If
        End While
        Console.WriteLine("Exit")
   
Continue

The continue statement (shown in Listing 5.35) is used to jump to the end of the loop immediately and process the next iteration of the loop.

Listing 5.35: Continue Example

While True
            '...
            If x = 0 Then
                x = 5
                Continue While
            End If
            '...
             If x = 5 Then
                Console.WriteLine("continue")
                '...
            End If
        End While

Return

The return statement is used to prematurely return from a method. The return statement can return empty or with a value on the stack, depending upon the return value definition in the method (Listing 5.36 shows both). Void methods do not require a return value. For other functions, you need to return an appropriate value of the type you declared in the method signature.

Listing 5.36: Return Example

Sub MyFunc1()
        ' ...
        If x = 1 Then
            Return
        End If
        ' ...
End Sub
     Function MyFunc2() As Integer
        ' ...
        If x = 2 Then
            Return 1919
        End If
        ' ...
    End Function

Conclusion

Hope this article would have helped you in understanding control statements in VB.NET.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.