The conditional statement IF ELSE , is use for examining the conditions that we provided, and making decision based on that contition. The conditional statement examining the data using comparison operators as well as logical operators.
If [your condition here] Your code here Else Your code Here End If
If the contition is TRUE then the control goes to between IF and Else block , that is the program will execute the code between IF and ELSE statements.
If the contition is FLASE then the control goes to between ELSE and END IF block , that is the program will execute the code between ELSE and END IF statements.
If you want o check more than one condition at the same time , you can use ElseIf .
If [your condition here] Your code here ElseIf [your condition here] Your code here ElseIf [your condition here] Your code here Else Your code Here End If
Just take a real-time example - When we want to analyze a mark lists we have to apply some conditions for grading students depends on the marks.
Following are the garding rule of the mark list:
1) If the marks is greater than 80 then the student get higher first class
2) If the marks less than 80 and greater than 60 then the student get first class
3) If the marks less than 60 and greater than 40 then the student get second class
4) The last condition is , if the marks less than 40 then the student fail.
Now here implementing these conditions in a VB.NET program.
1. If totalMarks >= 80 Then
2. MsgBox("Got Higher First Class ")
3. ElseIf totalMarks >= 60 Then
4. MsgBox("Got First Class ")
5. ElseIf totalMarks >= 40 Then
6. MsgBox("Just pass only")
7. Else
8. MsgBox("Failed")
9. End If
Line 1 : Checking the total marks greaterthan or equal to 80
Line 2 : If total marks greater than 80 show message - "Got Higher First Class "
Line 3 : Checking the total marks greaterthan or equal to 60
Line 4 : If total marks greater than 60 show message - "Got First Class "
Line 5 : Checking the total marks greaterthan or equal to 40
Line 6 : If total marks greater than 40 show message - "Just pass only"
Line 7 : If those three conditions failed program go to the next coding block
Line 8 : If all fail shows message "Failed"
Line 9 : Ending the condition block
VB.NET Source Code
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Dim totalMarks As Integer
totalMarks = 59
If totalMarks >= 80 Then
MsgBox("Gor Higher First Class ")
ElseIf totalMarks >= 60 Then
MsgBox("Gor First Class ")
ElseIf totalMarks >= 40 Then
MsgBox("Just pass only")
Else
MsgBox("Failed")
End If
End Sub
End Class
In this example the total marks is 59 , when you execute this program you will get in messagebox "Just Pass Only"
If you want to check a condition within condition you can use nested if statements
If [your condition here] If [your condition here] Your code here Else Your code Here End If Else Your code Here End If
Also you can write IF ELSE Statements in a single line
If [your condition here] [Code] Else [code]
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Dim totalMarks As Integer
totalMarks = 39
If totalMarks >= 50 Then MsgBox("passed ") Else MsgBox("Failed ")
End Sub
End Class