0% found this document useful (0 votes)
9 views9 pages

Lecture Four

Uploaded by

David Ezekiel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

Lecture Four

Uploaded by

David Ezekiel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Visual Basic Select Case Statement 1

In Visual Basic, Select...Case statement is useful to execute a single case statement from the
group of multiple case statements based on the value of a defined expression.

By using Select...Case statement in Visual Basic, we can replace the functionality of if…else if
statement to provide better readability for the code.
Visual Basic Select Case Statement Syntax 2
Generally, in Visual Basic, the Select...Case statement is a collection of
multiple case statements, and it will execute only one case statement based on
the matching value of the defined expression.
Following is the syntax of defining the Select...Case statement in Visual Basic programming language.

Select Case variable/expresison


Case value1
// Statements to Execute
Case value2
//Statements to Execute
....
....
Case Else
// Statements to Execute if No Case Matches
End Select
Visual Basic Select Case Statement Syntax 3

If you observe the above syntax, we defined a Select...Case statement with multiple case
statements. The Select statement will evaluate the expression / variable value by matching with
Case statement values (value1, value2, etc.). If the variable/expression value matches with any of
the case statements, the statements inside of that particular case will be executed.

If none of the case statements match the defined expression/variable value, then the statements
inside the Else block will be executed, and it’s more like Else block in the if...else statement.
Visual Basic Select Case Statement Flow Chart 4
Visual Basic Select Case Statement Flow Chart 5

If you observe the above Select...Case statement flow chart diagram, the process flow of
Select..Case statement will start from the Top to the Bottom, and in the first case, it will check
whether the expression value matches or not.

In case, if the expression value matches, it will execute the particular Case statement block and
execute the Select statement; otherwise, it will go to the second Case statement and check
whether the expression value is matching or not, the same way the search will continue till it
finds the right Case statement.

If all case statements fail to match with the defined expression value, then the Else block
statements will be executed, and the Select statement will come to an end.
Visual Basic Select Case Statement Flow Chart 6

Following is the example of using select...case statement in Visual


Basic programming language.
Module Module1

Sub Main()
Dim myInt As Integer = Nothing 7
Console.WriteLine("Please enter an integer")
myInt = Console.ReadLine()
Select Case myInt
Case 0
Console.WriteLine("Python programming")
Case 1
Console.WriteLine("VB.NET programming")
Case 2
Console.WriteLine("Programming for life")

End Select
Console.ReadLine()

End Sub

End Module
Module Module1

Sub Main()
8
Dim myInt As Integer = Nothing
Console.WriteLine("Please enter an Integer")
myInt = Console.ReadLine()
Select Case myInt
Case 0
Console.WriteLine("Hello")
Case 1
Console.WriteLine("Bye")
Case 2
Console.WriteLine("Goodmorning")

End Select
Console.ReadLine()
End Sub

End Module
Module Module1
Sub Main() 9
Dim x As Integer = 20
Select Case x
Case 10
Console.WriteLine("x value is 10")
Case 15
Console.WriteLine("x value is 15")
Case 20
Console.WriteLine("x value is 20")
Case Else
Console.WriteLine("Not Known")
End Select
Console.WriteLine("Press Enter Key to Exit..")
Console.ReadLine()
End Sub
End Module

You might also like