Control Structure in VB.NET


Sometimes in the program the situation may arise when you have to perform some action or take decision based on some condition at that time you need to use control structure.
Control structures are very useful for taking decisions based on the condition.
Control structures structure first test for the condition and based on the outcome of that condition they perform the specified tasks.
Following are the list of Control structures :
(1) If … Then … EndIf
(2) If … Then … Else … EndIf
(3) If … Then … ElseIf … EndIf
(4) Nested If … Then … EndIf
(5) IIF Statement
(6) Select Case….End Select

(1) If …Then … EndIf Statement


The general syntax of the If … Then …. EndIf structure is given below:

Syntax:
If condition Then
Statement Block
End if

It works as follow:
(1) First a condition is checked.
(2) If the condition is TRUE then the statement block will execute.
(3) If the condition is FALSE then the statement block will not execute and the control is transferred to the statement after the End If statement.

Example:
If txtName.text = “” then
MsgBox ("Please Enter Name")
txtName.Focus ()
End If
     

(2) If … Then … Else … End If


The General syntax of If … Then … Else … End If structure is given below:
Syntax:

If condition then
Statement block 1
Else
Statement block 2
End if

The above structure works as follow:
(1) First a condition is checked.
(2) If the condition is TRUE then it will execute the statement block 1.
(3) If the condition is FALSE then it will execute the statement block 2.
Example:

If val (txtNumber1.text) > val (txtNumber2.text) then
MsgBox (txtNumber1.text & “Is Maximum”)
Else
MsgBox (txtNumber2.text & “Is Maximum”)
End If

(3) If … Then … ElseIf … End If


The general syntax of If … Then … ElseIf … End If structure is given below:

If Condition1 Then
Statement Block 1
ElseIf Condition2 Then
Statement Block 2
ElseIf Condition3 Then
Statement Block 3
……..
ElseIf ConditionN Then
Statement Block N
Else
Default Statement Block
End if

The above structure works as follow:
(1) First Condition1 is checked.
(2) If the Condition1 is TRUE then it will execute statement block 1.
(3) If the Condition1 is FALSE then Condition2 is checked and the same process is repeated until any of the condition specified becomes TRUE.
(4) If all the condition evaluates to false then it will executes the default statement block followed by the else statement.
Example:

If txtName.text = “” then
MsgBox (“Please Enter Name”)
txtName. Focus ()
ElseIf txtAge.text = “” then
MsgBox (“Please Enter Age”)
txtAge. Focus ()
ElseIf txtBasic.text = “” then
MsgBox (“Please Enter Basic”)
txtBasic. Focus ()
Else
Basic = Val (txtBasic.text)
DA = Basic * 0.80
HRA = (Basic + DA) * 0.12
Gross = Basic + DA + HRA
txtGross.text = Gross
End If

(4) Nested If …then


When one If … Then … End If statement is contained within another If … Then … End If statement then it is known as Nested If … Then … End If structure. The general syntax for Nested If … Then … End If structure is given below:

If condition-1 then
If condition-2 then
Statement block 1
Else
Statement block 2
End if
Else
Statement block 3
End if

The above structure works as follow:
(1) First Condition1 is checked.
(2) If condition1 is TRUE then it tests for condition2.
(3) If condition2 is TRUE then it executes statement block 1.
(4) If condition2 is FALSE then it executes statement block 2.
(5) If condition1 is FALSE then it executes statement block 3.
Example:

If txtBasic.text <> “” then
If IsNumeric (txtBasic.text) then
Basic = txtBasic.text
Else
MsgBox (“Please Enter Numeric Value”)
txtBasic.Focus ()
Else
MsgBox (“Please Enter Basic”)
txtBasic.Focus ()
End If

(5) IIF statement:


IIF statement is the short form of If… then … else statement. The general syntax of IIF statement is given below:

IIF (expression, TRUE Part, FALSE Part)

The above statement works as follow:
(1) First an expression is evaluated.
(2) If the expression evaluates to TRUE then it returns the TRUE Part value.
(3) If the expression evaluates to FALSE then it returns the FALSE Part value.
Example:

Dim A as integer, B as Integer, Max as Integer
A = 5
B = 6
Max = IIF (A > B, A, B)
Here the condition is FALSE, so it returns B and assign to Max.

(6) Select Case … End Select


It is also known as multiple choice decision statement. It allows you to select one option from the list of available options. It is the alternative of If …Then ... ElseIf structure. The general syntax for Select Case …. End Select structure is given below:

Select Case expression
Case Value1
Statement Block 1
Case Value 2
Statement Block 2
…………………………
Case Value N
Statement Block N
Case Else
Default statement Block
End Select

The above structure works as follow:
(1) It compares the value of expression against the list of values specified in the different case values. When the match is found it executes the statement block associated with that case value.
(2) If no match is found then the default statement associated with Case Else will executes.
Example:

Dim A as integer, B as integer, C as Integer
Dim op as string
Select Case op
Case “+”
C = A + B
Case “-”
C = A - B
Case “*”
C = A * B
Case “/”
C = A / B
Case Else
MsgBox (“Wrong Option”)
End Select

Some Features of Select Case … End Select structure: (1) If you want to specify the action to be taken on the values between specified ranges then you can also specify range in the case value as shown below:

Select Case Age
Case 1 To 6
MsgBox (“Kid”)
Case 7 To 18
MsgBox (“Tin Age”)
Case 19 To 100
MsgBox (“Adult”)
End Select

(2) You can also specify multiple values with the Case as shown below:

Select Case Grade
Case 10, 9
MsgBox (“Excellent”)
Case 8, 7
MsgBox (“Very Good”)
Case 6, 5
MsgBox (“Good”)
Case Else
MsgBox (“Poor”)
End Select

(3) You can also use relational operator to specify compare case values as shown below:

Select Case Percentage
Case Is >= 66
MsgBox (“Distinction”)
Case Is >=60
MsgBox (“First Class”)
Case Is >=50
MsgBox (“Second Class”)
Case Is >=35
MsgBox (“Pass Class”)
Case Else
MsgBox (“Fail”)
End Select

Download Projects


Download Programs