ARTICLE

Conditional Statements in Visual Basic .NET

Posted by Atul Chaudhary Articles | Visual Basic 2010 December 29, 2010
In this article, I will explain you Conditional Statements in Visual Basic .NET
 
Reader Level:

In this article, I will explain you about Conditional Statements in Visual Basic .NET

If....Else Statement


The control statements which allows us if a condition is true execute a expression and if it is False execute a different expression is If conditional expression. The expression followed by Then keyword will be executed if the condition is true, otherwise the expression followed by
ElseIf keyword will be checked and if it is true then that expression will be executed.

Example:

Imports System.Console
Module Module1
 
    Sub Main()
        Dim A As Integer
        Write("Enter a Number, 10 or 20 or 30")
        A = Val(ReadLine())
      
'ReadLine() method is used to read from console

         If A = 10 Then
            WriteLine("Ten")
        ElseIf A = 20 Then
            WriteLine("Twenty")
        ElseIf A = 30 Then
            WriteLine("Thirty")
        Else
            WriteLine("Number not 10,20,30")
        End If
        Read()
 
    End Sub
 
End Module

Output of the above code is given below:

output1.gif

Select….Case Statement

The Select Case control structure is nearly same as the If....ElseIf control structure. Select Case Statement can make decision only on one statement. Select Case is preferred when your code has the ability to handle many conditions of a particular variable. Select Case is use to test the expression, which of the given cases it matches and execute the code in that expression.

Example:

Imports System.Console
Module Module1
 
    Sub Main()
        Dim Password As Integer
        Write("Enter a number between 1 and 6")
        Password = Val(ReadLine())
 
        Select Case Password
            Case 1
                WriteLine("You entered 1")
            Case 2
                WriteLine("You entered 2")
            Case 3
                WriteLine("You entered 3")
            Case 4
                WriteLine("You entered 4")
            Case 5
                WriteLine("You entered 5")
            Case 6
                WriteLine("You entered 6")
 
        End Select
        Read()
    End
Sub

Output of the above code is given below:

output2.gif

Summary

I hope this article help you to understand Conditional Statements in Visual Basic .NET

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • 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.
    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!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor