Control Statements in VB.NET: Part 1

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

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

If-then-else

The if statement has three forms: single selection, if-then-else selection, and multicase selection. Listing 5.23 contains an example of each form.

Listing 5.23: If-Else-ElseIf Example 1

        'single selection
        If i > 0 Then
            Console.WriteLine("The number {0} is positive", i)
        End If
        'if-then-else selection
        If i > 0 Then
            Console.WriteLine("The number {0} is positive", i)
        Else
            Console.WriteLine("The number {0} is not positive", i)
        End If
        'multicase selection
        If i = 0 Then
            Console.WriteLine("The number is zero")
        ElseIf i > 0 Then
            Console.WriteLine("The number {0} is positive", i)
        Else
            Console.WriteLine("The number {0} is negative", i)
        End If
 
The variable i is the object of evaluation here. The expression in an if statement must resolve to a boolean value type.

        ' Compiler Error
        If 1 Then
             
Console.WriteLine("The if statement executed")
        End If
        Console.ReadLine()

When the VB.NET compiler compiles the preceding code, it generates the error "Constant value 1 cannot be converted to bool."

Listing 5.24 shows how conditional or (OrElse) and conditional and (AndAlso) operators are used in the same manner.

Listing 5.24: If-Then-Else Example 2


        'Leap year
        Dim year As Integer = 1974
        If (year Mod 4 = 0 AndAlso year Mod 100 <> 0) OrElse year Mod 400 = 0 Then
            Console.WriteLine("The year {0} is leap year ", year)
        Else
            Console.WriteLine("The year {0} is not leap year ", year)
        End If

Switch

From the example in Listing 5.25, you can see that the select case is similar to an if-else ifelse if-else form of an if statement.

Listing 5.25: Switch Example 1

        Dim day As String = "Monday"
        Console.WriteLine("enter the day :")
        day = Console.ReadLine()
        Select Case day
            Case "Mon"
                Exit Select
            Case "Monday"
                Console.WriteLine("day is Monday: go to work")
                Exit Select
            Case Else
                Console.WriteLine("default")
                Exit Select
        End Select
        Select Case strVal1
            Case "reason1"
              goto case "reason2"
                ' this is a jump to mimic fall-through
            Case "reason2"
                intOption = 2
                Exit Select
            Case "reason 3"
                intOption = 3
                Exit Select
            Case "reason 4"
                intOption = 4
                Exit Select
            Case "reason 5"
                intOption = 5
                Exit Select
            Case Else
                intOption = 9
                Exit Select

        End Select

Conclusion

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

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.