Nested If Then Else Structure
lets take a look at nested if then else structure in visual basic. When we compare condition while staying in a condition comparison or we create a if structure within an if structure is called nested structure.
A condition located inside a condition is called nested condition. When programming logics clear first step and again have to execute a condition we can handle it using nested If then else statement.
Every nested statement closes itself inside the parent statement I mean outer statement. And can have its own complete parameters including else too.
While in extended statements we use next comparison in else and again next it in next else. its nested because whole next comparison is executed completely staying in its outer statement.
lets see the syntax to get more better understanding.
Write a program to compare numbers of two subjects for a student. If both subjects have marks more then 70 then to show BEST otherwise Bad.
Start a new EXE project and design the form as below:
Set all properties of controls as required in project we already have discussed about properties.
Double click to command button to open the code window and type the code as below:
— defining variables
Dim Science As Integer Dim ComputerScience As Integer
–getting values from text boxes and saving in variables
Science = Text1.Text ComputerScience = Text2.Text
— outer of nested comparison
If Science >= 70 Then
— inner of nested comparison
If ComputerScience >= 70 Then MsgBox "Best" Else MsgBox "Better" End If Else MsgBox "Bad" End If
Practice makes the man perfect so keep practicing.