Control
Structure in VB.NET
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
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
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.
(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
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:
Syntax:
If condition then
Statement block 1
Else
Statement block 2
End if
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:
(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
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
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:
(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
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
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:
(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
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:
(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.
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
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:
(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
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
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
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
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
Looping Control
Structure
Sometimes it is required to perform some action or task repeatedly
for specified number of times or until some condition is satisfied at that time
you need to use Looping control structure. Following are the List of Looping
Control Structure:
(1) Do While … Loop
(2) Do Until …. Loop
(3) Do … Loop While
(4) Do … Loop Until
(5) For … Next
(6) For Each … Next
(7) While … End While
(1) Do While … Loop
(2) Do Until …. Loop
(3) Do … Loop While
(4) Do … Loop Until
(5) For … Next
(6) For Each … Next
(7) While … End While
(1) Do While ….
Loop
Do While … Loop control structure is used to repeat statements
between Do While and Loop statements until the condition specified with the Do
While statements evaluates to FALSE. The general syntax of Do While … Loop
structure is given below:
Do while Condition
Statement Block
Loop
Statement Block
Loop
The above structure works as follow:
(1) First it tests for condition specified with Do While statement.
(2) If the condition evaluates to TRUE then the statement block within Do While and Loop is executed. After executing the statement block the condition is tested again and the same process is repeated until the condition becomes FALSE.
(3) If the condition evaluates to FALSE then the statements between Do While and Loop is not executed and control is transferred after the statement Loop.
If the condition evaluates to FALSE at the first trial the statements between Do While and Loop statements will never executes.
It is also known as Entry Controlled Loop or Pretest Loop because the condition is checked before executing the statements.
Example:
Suppose you want to display odd numbers in the range 1 to 10.
(1) First it tests for condition specified with Do While statement.
(2) If the condition evaluates to TRUE then the statement block within Do While and Loop is executed. After executing the statement block the condition is tested again and the same process is repeated until the condition becomes FALSE.
(3) If the condition evaluates to FALSE then the statements between Do While and Loop is not executed and control is transferred after the statement Loop.
If the condition evaluates to FALSE at the first trial the statements between Do While and Loop statements will never executes.
It is also known as Entry Controlled Loop or Pretest Loop because the condition is checked before executing the statements.
Example:
Suppose you want to display odd numbers in the range 1 to 10.
Dim a As Integer
a = 1
Do While a <= 10
If a Mod 2 <> 0 Then
lblOdd.Text = lblOdd.Text & " " & a
End If
a = a + 1
Loop
a = 1
Do While a <= 10
If a Mod 2 <> 0 Then
lblOdd.Text = lblOdd.Text & " " & a
End If
a = a + 1
Loop
|
|
|
(2) Do Until ….
Loop
Do Until … Loop control structure is used to repeat statements
between Do Until and Loop statements until the condition specified with the Do
Until statements evaluates to TRUE. The general syntax of Do Until … Loop
structure is given below:
Do Until Condition
Statement Block
Loop
Statement Block
Loop
The above structure works as follow:
(1) First it tests for condition specified with Do Until statement.
(2) If the condition evaluates to FALSE then the statement block within Do Until and loop is executed. After executing the statement block the condition is tested again and the same process is repeated until the condition becomes TRUE.
(3) If the condition evaluates to TRUE then the statements between Do Until and Loop is not executed and control is transferred after the statement Loop.
Example:
Suppose you want to display odd numbers in the range 1 to 10.
(1) First it tests for condition specified with Do Until statement.
(2) If the condition evaluates to FALSE then the statement block within Do Until and loop is executed. After executing the statement block the condition is tested again and the same process is repeated until the condition becomes TRUE.
(3) If the condition evaluates to TRUE then the statements between Do Until and Loop is not executed and control is transferred after the statement Loop.
Example:
Suppose you want to display odd numbers in the range 1 to 10.
Dim a As Integer
a = 1
Do Until a >= 10
If a Mod 2 <> 0 Then
lblOdd.Text = lblOdd.Text & " " & a
End If
a = a + 1
Loop
a = 1
Do Until a >= 10
If a Mod 2 <> 0 Then
lblOdd.Text = lblOdd.Text & " " & a
End If
a = a + 1
Loop
(3) Do … Loop While
Do … Loop While control structure is used to repeat Statement
Block until the condition evaluates to FALSE. The general syntax of Do … Loop
While structure is given below:
Do
Statement Block
Loop While condition
Statement Block
Loop While condition
The above structure works as follow:
(1) In this structure first Statement Block is executed.
(2) After executing the Statement Block it tests for the condition.
(3) If the condition evaluates to TRUE then the Statement Block is executed. After executing the Statement Block the condition is tested again and the same process is repeated until the condition becomes FALSE.
(4) If the condition evaluates to FALSE then the Statement Block is not executed and control is transferred after the Loop. Thus in Do …. Loop While structure the Statement Block executes at least once even the condition is FALSE at the First trial.
It is also known as Exit Controlled Loop or Posttest Loop because the condition is checked after executing the statement block.
Example:
Suppose you want to display odd numbers in the range 1 to 10
(1) In this structure first Statement Block is executed.
(2) After executing the Statement Block it tests for the condition.
(3) If the condition evaluates to TRUE then the Statement Block is executed. After executing the Statement Block the condition is tested again and the same process is repeated until the condition becomes FALSE.
(4) If the condition evaluates to FALSE then the Statement Block is not executed and control is transferred after the Loop. Thus in Do …. Loop While structure the Statement Block executes at least once even the condition is FALSE at the First trial.
It is also known as Exit Controlled Loop or Posttest Loop because the condition is checked after executing the statement block.
Example:
Suppose you want to display odd numbers in the range 1 to 10
Dim a As Integer
a = 1
Do
If a Mod 2 <> 0 Then
lblOdd.Text = lblOdd.Text & " " & a
End If
a = a + 1
Loop While a <= 10
a = 1
Do
If a Mod 2 <> 0 Then
lblOdd.Text = lblOdd.Text & " " & a
End If
a = a + 1
Loop While a <= 10
(4) Do … Loop Until
Do … Loop Until control structure is used to repeat Statement
Block until the condition specified evaluates to TRUE. The general syntax of Do
… Loop Until structure is given below:
Do
Statement Block
Loop Until condition
Statement Block
Loop Until condition
The above structure works as follow:
(1) In this structure first Statement Block is executed.
(2) After executing the Statement Block it tests for the condition.
(3) If the condition evaluates to FALSE then the Statement Block is executed. After executing the Statement Block the condition is tested again and the same process is repeated until the condition becomes TRUE.
(4) If the condition evaluates to TRUE then the Statement Block is not executed and control is transferred after the statement Loop.
Thus in Do …. Loop until structure the statement block executes at least once even the condition is TRUE at the First trial.
Example:
Suppose you want to display odd numbers in the range 1 to 10:
(1) In this structure first Statement Block is executed.
(2) After executing the Statement Block it tests for the condition.
(3) If the condition evaluates to FALSE then the Statement Block is executed. After executing the Statement Block the condition is tested again and the same process is repeated until the condition becomes TRUE.
(4) If the condition evaluates to TRUE then the Statement Block is not executed and control is transferred after the statement Loop.
Thus in Do …. Loop until structure the statement block executes at least once even the condition is TRUE at the First trial.
Example:
Suppose you want to display odd numbers in the range 1 to 10:
lblOdd.Text = ""
Dim a As Integer
a = 1
Do
If a Mod 2 <> 0 Then
lblOdd.Text = lblOdd.Text & " " & a
End If
a = a + 1
Loop Until a >= 10
Dim a As Integer
a = 1
Do
If a Mod 2 <> 0 Then
lblOdd.Text = lblOdd.Text & " " & a
End If
a = a + 1
Loop Until a >= 10
(5) For…Next
For…Next control structure is used to repeat Statement Block for
specified number of times. The general syntax of For … Next statement is given
below:
For counter=Start-Value To End-Value [step Step-Value]
Statement Block
Next
Here,
Start-Value: It indicates from which value the loop will start.
End-Value: It indicates at which value the loop will stop.
Step-Value: it is optional. It indicates by which value the counter will increment or decrement. If not specified then by default its value is 1. You can specify either positive or negative value.
Note: When step value is positive then the start value must be less then the end value. When step value is negative then the start value must be greater then end value.
Statement Block
Next
Here,
Start-Value: It indicates from which value the loop will start.
End-Value: It indicates at which value the loop will stop.
Step-Value: it is optional. It indicates by which value the counter will increment or decrement. If not specified then by default its value is 1. You can specify either positive or negative value.
Note: When step value is positive then the start value must be less then the end value. When step value is negative then the start value must be greater then end value.
The For … Next statement is useful when you know in advance how
many times you wants to executes the statement block repeatedly.
The above structure works as follow:
(1) First it initializes the counter with the start value.
(2) Executes the statement block.
(3) After executing the Statement Block it will increment or decrement the counter with the value specified in the step value. If step value is not specified then by default it increment or decrement counter with 1.
This process is repeated until counter reaches to the End Value.
Example:
Suppose you want to find factorial of number 4:
The above structure works as follow:
(1) First it initializes the counter with the start value.
(2) Executes the statement block.
(3) After executing the Statement Block it will increment or decrement the counter with the value specified in the step value. If step value is not specified then by default it increment or decrement counter with 1.
This process is repeated until counter reaches to the End Value.
Example:
Suppose you want to find factorial of number 4:
Dim i As Integer
Dim n As Integer = 4
Dim fact As Integer = 1
For i = 1 To n Step 1
fact = fact * i
Next
Label1.Text = fact
Dim n As Integer = 4
Dim fact As Integer = 1
For i = 1 To n Step 1
fact = fact * i
Next
Label1.Text = fact
(6) For Each…Next:
For each … Next control structure repeats the Statement Block for
all the elements in the array or collection. So there is no need to know the
start value and the end value.
The general syntax of For Each … Next control structure is given below:
The general syntax of For Each … Next control structure is given below:
For Each elements In Array/Collection
Statement Block
Next
Statement Block
Next
Example: Suppose you want to multiply each element of array by 2
and then display it.
Label1.Text = ""
Dim i As Integer
Dim n() As Integer = {1, 2, 3, 4, 5}
For Each i In n
i = i * 2
Label1.Text = Label1.Text & " " & i
Next
Dim i As Integer
Dim n() As Integer = {1, 2, 3, 4, 5}
For Each i In n
i = i * 2
Label1.Text = Label1.Text & " " & i
Next
(7) While … End
While
The general syntax of while …. End While structure is given below:
While condition
Statement Block
End While
Statement Block
End While
While … End While control structure is used to repeat statement
block until the specified condition evaluates to false.
The above structure works as follow:
(1) First it tests for condition.
(2) If the condition evaluates to TRUE then the statement block is executed. After executing the statement block the condition is tested again and the same process is repeated until the condition becomes FALSE.
(3) If the condition evaluates to FALSE then the statement block is not executed and control is transferred after End While statement.
Example:
Suppose you want to display odd numbers in the range 1 to 10.
The above structure works as follow:
(1) First it tests for condition.
(2) If the condition evaluates to TRUE then the statement block is executed. After executing the statement block the condition is tested again and the same process is repeated until the condition becomes FALSE.
(3) If the condition evaluates to FALSE then the statement block is not executed and control is transferred after End While statement.
Example:
Suppose you want to display odd numbers in the range 1 to 10.
lblOdd.Text = “”
Dim a As Integer
a = 1
While a < 10
If a Mod 2 <> 0 Then
lblOdd.Text = lblOdd.Text & " " & a
End If
a = a + 1
End While
Dim a As Integer
a = 1
While a < 10
If a Mod 2 <> 0 Then
lblOdd.Text = lblOdd.Text & " " & a
End If
a = a + 1
End While
Comments
Post a Comment