Net-informations.com

How to use IF ELSE in VB.NET

if_else_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.

  1. Line 1 : Checking the total marks greaterthan or equal to 80.
  2. Line 2 : If total marks greater than 80 show message - "Got Higher First Class ".
  3. Line 3 : Checking the total marks greaterthan or equal to 60.
  4. Line 4 : If total marks greater than 60 show message - "Got First Class ".
  5. Line 5 : Checking the total marks greaterthan or equal to 40.
  6. Line 6 : If total marks greater than 40 show message - "Just pass only".
  7. Line 7 : If those three conditions failed program go to the next coding block.
  8. Line 8 : If all fail shows message "Failed".
  9. 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.






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

When you execute this program you will get in messagebox "Failed "






net-informations.com (C) 2019    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About