Nested If Then Else Statement in VB.NET

How to use nested if then else statement in VB.NET?

Explanation

Nested If Then Else Statement

Nested If..Then..Else statement is used to check multiple conditions using if then else statements nested inside one another.
Syntax:
If [Condition] Then
If [Condition] Then
[Statements]
Else
[Statements] Else
[Statements]

In the above syntax when the Condition of the first if then else is true, the second if then else is executed to check another two conditions. If false the statements under the Else part of the first statement is executed.
Example:

Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
If Val(TextBox1.Text) >= 40 Then
If Val(TextBox1.Text) >= 60 Then
MsgBox("You have FIRST Class")
Else
MsgBox("You have SECOND Class")
End If
Else
MsgBox("Check your Average marks entered")
End If
End Sub
Description:

In the above nested if then else statement example first the average mark is checked if it is more than 40, if true the second if then else control is used check for first or second class. If the first condition is false the statements under the else part is executed.

Visual Basic Tutorial


Ask Questions

Ask Question