How to use IF ELSE in VB.NET
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 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 .
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.
- 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
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.
Also you can write IF ELSE Statements in a single line.
When you execute this program you will get in messagebox "Failed "