In this blog we will see alternate to If Else If Structure i.e Select Case statement
Syntax
Select Case Text Expression
Case Expression1
Statment1
Case Expression2
Statement2
Else
Statement
End Select
Let us see an example using Select Case Statement.
Module Example10
Sub Main()
Dim MonthNum As Integer
Console.WriteLine("Enter Month Name:")
MonthNum = Console.ReadLine()
Select Case MonthNum
Case 1
Console.WriteLine("January")
Case 2
Console.WriteLine("February")
Case 3
Console.WriteLine("March")
Case 4
Console.WriteLine("April")
Case 5
Console.WriteLine("May")
Case 6
Console.WriteLine("June")
Case 7
Console.WriteLine("July")
Case 8
Console.WriteLine("August")
Case 9
Console.WriteLine("September")
Case 10
Console.WriteLine("October")
Case 11
Console.WriteLine("November")
Case 12
Console.WriteLine("December")
Case Else
MsgBox("Invalid Input")
End Select
Console.ReadKey()
End Sub
End Module
The above example is rewriteen of If Else If Example. You can use If Else If or Select Case nothing is difference in performance.
Syntax
Select Case Text Expression
Case Expression1
Statment1
Case Expression2
Statement2
Else
Statement
End Select
Let us see an example using Select Case Statement.
Module Example10
Sub Main()
Dim MonthNum As Integer
Console.WriteLine("Enter Month Name:")
MonthNum = Console.ReadLine()
Select Case MonthNum
Case 1
Console.WriteLine("January")
Case 2
Console.WriteLine("February")
Case 3
Console.WriteLine("March")
Case 4
Console.WriteLine("April")
Case 5
Console.WriteLine("May")
Case 6
Console.WriteLine("June")
Case 7
Console.WriteLine("July")
Case 8
Console.WriteLine("August")
Case 9
Console.WriteLine("September")
Case 10
Console.WriteLine("October")
Case 11
Console.WriteLine("November")
Case 12
Console.WriteLine("December")
Case Else
MsgBox("Invalid Input")
End Select
Console.ReadKey()
End Sub
End Module
The above example is rewriteen of If Else If Example. You can use If Else If or Select Case nothing is difference in performance.
No comments:
Post a Comment