Conditional Statements in Visual Basic .NET

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

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:

output5.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:

output6.gif

Summary

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

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.