Conditional statements available in Visual Basic.Net.
Conditional statements available in Microsoft Visual Basic.Net. The various conditional statements in Microsoft Visual Basic.Net are if conditional statement, if-else conditional statement, if-elseif-else conditional statement,select case etc.
In the last article I showed you how to use with statement and various loops available in Visual Basic.Net. In this article I am going to show you the conditional statements available in Microsoft Visual Basic.Net.
If-else:-
This is used in making decisions in VB.Net. If a condition is true then the statements following if will be executed otherwise statements following else will be executed.
If-else-if: -
If a condition is true then the statements following if will be executed otherwise if the second condition is ture then the statements following the corresponding if statements will be executed if all the conditions are flase then the statements following else will be executed.
Select Case: -This is used to test an expression to determine which of the several cases it matches and then executes the corresponding code.
Public Class Form1
Private Sub btn_if_else_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_if_else.Click
Dim str As String
str = TextBox1.Text
If str = "hello" Then
MsgBox("If part is executed")
Else
MsgBox("Else Part is executed")
End If
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_if_elseif_else.Click
Dim str As String
str = TextBox1.Text
If str = "hello" Then
MsgBox("If part is executed")
ElseIf str = "Bye" Then
MsgBox(" ElseIf Part is executed")
Else
MsgBox("Else Part is executed")
End If
End Sub
Private Sub btn_if_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_if.Click
Dim str As String
str = TextBox1.Text
If str = "hello" Then
MsgBox("If is executed")
End If
End Sub
Private Sub btn_selectcase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_selectcase.Click
Dim day As String
day = TextBox1.Text
Select Case day
Case ("Mon")
MsgBox("You have entered Monday")
Case ("Tue")
MsgBox("You have entered Tuesday")
Case ("Wed")
MsgBox("You have entered Wednesday")
Case ("Thu")
MsgBox("You have entered Thursday")
Case ("Fri")
MsgBox("You have entered Friday")
Case ("Sat")
MsgBox("You have entered Saturday")
Case ("Sun")
MsgBox("You have entered Sunday")
Case Else
MsgBox("You have entered WrongDay")
End Select
End Sub
End Class
Regards