Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » Visual Basic Language » Control Statements in VB.NET: Part 1

Control Statements in VB.NET: Part 1

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

Author Rank :
Page Views : 1477
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Team Foundation Server Hosting
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Manish Tewatia
Manish is Very much interested in Microsoft & LifeStyle Accessory Designand working with Microsoft technologies. His expert areas are ASP.NET, ADO.NET, C# .NET, WPF, WCF, Windows Phone 7, Android, SQL Server, HTML , XAML, etc…
He is doing MCA and he love to drive fast bikes.
inspired from Mr. Mahesh Chand.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
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.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Comments
DevExpress Free UI Controls
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.