ARTICLE

Control Statements in VB.NET: Part 1

Posted by Manish Tewatia Articles | Visual Basic Language August 29, 2010
In this article I will explain your about different control statements in VB.NET.
 
Reader Level:

HTML clipboard

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.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Diagram
Become a Sponsor