In any programming languages, the control statements are the statements that are used to control the execution of the program on the basis of the specified condition.
In Visual Basic.Net, control statement is useful for determining whether an executed condition is true or false. If after the execution, the condition happens to be true, a block or single of statement is executed.
Control statements include, If- Then, If Then Else, If Then ElseIf and the Select Case statement.
We can define more than one condition to be evaluated by the program with statements. If the defined condition is true, the statement or block executes according to the condition, and if the condition is false, another statement is executed vise versa.
In Visual Basic, we have If-Then Statement, If-Then Else Statement, If-Then ElseIf Statement, Select Case Statement, Nested Select Case Statements
The If-Then Statement is a control statement that defines one or more conditions, and if the particular condition is satisfied, it executes a piece of information or statements.
If expression Then
'code to execute
End If
In If-Then Statement, the condition can be a Boolean, logical, or relational condition, and the statement can be single or group of statements that will be executed when the condition is met.
We are going to write a simple program that will grant access to users to vote based on their age. Above eighteen years is eligible to vote.
Module IfThenStatement
Sub Main()
Dim age As Integer
Console.WriteLine("Enter age")
age = Int(Console.ReadLine())
If age >= 18 Then
Console.WriteLine("You are eligible to vote")
End If
If age < 18 Then
Console.WriteLine("You are not eligible to vote")
End If
Console.WriteLine("Press any key to terminate")
Console.ReadKey()
End Sub
End Module
Now compile and execute the above program by clicking on the Start or pressing F5 button. You should either get eligible or not eligible based on your age.
The If-Then Statement can execute single or multiple statements when the condition is true, but when the expression evaluates to false, it does nothing. So, here comes the If-Then-Else Statement. The IF-Then-Else Statement is telling what If condition to do when if the statement is false, it executes the Else statement.
If expression Then
'code to execute
Else
'Code to execute
End If
We are still going to work on the voting eligibility using If-Then Else Statement
Module IfThenElse
Sub Main()
Dim age As Integer
Console.WriteLine("Enter age")
age = Int(Console.ReadLine())
If age >= 18 Then
Console.WriteLine("You are eligible to vote")
Else
Console.WriteLine("You are not eligible to vote")
End If
Console.WriteLine("Press any key to terminate")
Console.ReadKey()
End Sub
End Module
Now press F5 key to run the program and input value to see the action.
If Then ElseIf statement gives you a choice to execute only one condition or statement from multiple conditions.
The execution always starts from the top to bottom, and it matches each of the conditions with the given parameters. And if the condition is met in any of the block of If statement, then that block of code is executed. And if none of the conditions are true, the last block which Else is executed.
If expression-1 Then
'Code to execute
ElseIf expression-2 Then
'Code to execute
ElseIf(expression-3)Then
'Code to execute
Else
' code to execute
End If
We are still going to work on the voting eligibility check. This time, the program will be able to tell the user number of years remaining for him or her to vote.
Module IfThenElseIf
Sub Main()
Dim age As Integer
Console.WriteLine("Enter age")
age = Int(Console.ReadLine())
If age >= 18 Then
Console.WriteLine("You are eligible to vote")
ElseIf age >= 16 And age <=17 Then
Console.WriteLine("You are not eligible to vote. You will be eligible in a 2 year time")
ElseIf age >= 17 And age <18 Then
Console.WriteLine("You are not eligible to vote. You will be eligible in a year time")
ElseIf age >= 10 And age <=11 Then
Console.WriteLine("You are not eligible to vote. You will be eligible in a 7 year time")
ElseIf age >= 9 And age <=10 Then
Console.WriteLine("You are not eligible to vote. You will be eligible in a 8 year time")
Else
Console.WriteLine("You are not eligible to vote")
End If
Console.WriteLine("Press any key to terminate")
Console.ReadKey()
End Sub
End Module
In Visual Basic.Net, the Select Case statement is a collection of multiple case statements, which allows executing a single case statement from the list of statements.
A selected case statement uses a variable to test for equality against multiple cases or statements in a program. If the variable is matched with any test cases, that statement will be executed. And if the condition is not matched with any cases, it executes the default statement.
Using the select case statement in VB.NET programming, you can replace the uses of multiple If-Then-Else If statement from the program for better readability and easy to use.
Following is the syntax of the Select Case statement in Visual Basic
Select Case [expression]
Case option 1
'code to execute
Case option 2
'code to execute
Case Else
'code to execute
End Select
Furthermore, you can also set more than one condition in a single case statement as below
Select Case [expression]
Case option 1
'code to execute
Case option 2, option 3
'code to execute
Case Else
'code to execute
End Select
In this example, you are expected to enter number 0-5 and the program will output the corresponding alphabet.
Module SelectCaseStatement
Sub Main()
Dim num As Integer
Console.WriteLine("Enter any between number 1-5 to display corresponding alphabet")
num = Int(Console.ReadLine())
Select Case num
Case 1
Console.WriteLine("1 is equivalent of A")
Case 2
Console.WriteLine("2 is equivalent of B")
Case 3
Console.WriteLine("3 is equivalent of C")
Case 4
Console.WriteLine("4 is equivalent of D")
Case 5
Console.WriteLine("5 is equivalent of E")
Case Else
Console.WriteLine("Invalid input")
End Select
Console.WriteLine("Press any key to terminate")
Console.ReadKey()
End Sub
End Module
Now compile and execute the above program by clicking on the Start or press F5 button to see the result.
When a Select Case statement is written inside the body of another Select Case statement is called a nested Select Case statement.
Select Case value
Case n1
'code to execute
' nested Select case
Select Case m1
' code to be executed if m condition is met
Case n2
'code to execute
Case m2
'code to execute if m2 condition is met
Case Else
'code to execute
End Select
Module SelectCaseNestedStatement
Sub Main()
Dim x As Integer = 10, y As Integer = 5
Select Case x
Case 10
Console.WriteLine("X Value: 10")
Select Case y
Case 5
Console.WriteLine("Nested Switch Value: 5")
Select Case y - 2
Case 3
Console.WriteLine("Another Nested Switch Value: 3")
End Select
End Select
Case 15
Console.WriteLine("X Value: 15")
Case 20
Console.WriteLine("X Value: 20")
Case Else
Console.WriteLine("Not Known")
End Select
Console.WriteLine("Press Enter Key to Exit..")
Console.ReadLine()
End Sub
End Module
Now compile and execute the above program by clicking on the Start or pressing F5 button to see the results