Nested Selection Structure, If-Then and Select case Statement
If-Then Statement:
If-Then Selection structure performs an action if condition is True and if not True then it skips the action. Syntax of If-Then Statement is
If Condition Then
Statement(s) or If Condition Then Statement
End If
Where Condition may be any expression, which can evaluate True (Non Zero) or False (Zero) result.
Example:
Start Visual Basic with new Standard EXE project, place a CommandButton on a Form and write following code in Click event of the CommandButton.
Private Sub Command1_Click ()
Dim i As
Integer, j As Integer
i = 5
j = 8
If (j
< i) Then
Print “Large No is =” , i
End If
If (j >
i) Then Print “Large No is = “ , j
End Sub
|
Output:
Large No is
= 8
|
Press F5 key to run this project and Click the CommandButton. And check the result.
If - Then - Else:
If - Then - Else statement is used to perform an action if condition is True and performs another different action if condition is False. Syntax of if - Then - Else statement is
If Condition Then
Statement (s)
Else
Statement (s)
End If
Example:
Start Visual Basic with new Standard EXE project, place a CommandButton on a Form and write following code in Click event of the CommandButton.
Private Sub Command1_Click ()
Dim I As
Integer, J As Integer
I = 5
J = 10
If I
> J Then
Print “Large No is =” , I
Else
Print “Large
No is = “ , J
End If
End Sub
|
Output:
Large No is
= 10
|
Press F5 key to run this project and Click the CommandButton. And check the result.
Nested Selection Structure:
When one selection structure is the part of another Selection structure, Then it is called nested selection structure. Syntax of nested IF statement is
If Condition1 Then Or If Condition1 Then
Statement(s) Block 1 Statement(s) Block 1
Else ElseIf condition2 Then
If condition2 Then Statement(s) Block 2
Statement(s) Block ----------------------------
--------------------------- ElseIf condition n Then
Else Statement(s) Block n
If condition n Then End If
Statement(s) Block n
End If
End If
End If
Example:
Start Visual Basic with new Standard EXE project, place a CommandButton on a Form and write following code in Click event of the CommandButton.
Private Sub Command1_Click ()
Dim I As
Integer, J As Integer
I = 5
J = 7
K = 6
If I
> J And I > K Then
Print “Large No is =” , I
Else
If J
> I And J > K Then
Print “Large
No is = “ , J
Else
If K >
J And K > I Then
Print “Large
No is = “ , K
End If
End If
End If
End Sub
|
Private Sub Command1_Click ()
Dim I As
Integer, J As Integer
I = 5
J = 7
K = 6
If I
> J And I > K Then
Print “Large No is =” , I
ElseIf J
> I And J > K Then
Print “Large No is = “ , J
ElseIf K
> J And K > I Then
Print
“Large No is = “ , K
End If
End Sub
|
Press F5 key to run this project and Click the CommandButton. Output for both code segments will be the same as displayed on the Form.
Large No is 7
Selection Case Statement:
Like If - Then - Else, select case is also multiple selection structure. Syntax of Select Case Statement is
Select Case Expression
Case ExpressionList 1
Statement(s) Block 1
Case ExpressionList 2
Statement(s) Block 2
-----------------------------------------
Case ExpressionList n
Statement(s) Block n
Case Else
Statement(s)
End Select
Where Expression may be any numeric or string expression and ExpressionList may be in the following Format.
- Expression 1 [, Expression 2 [, Expression 3]].....
- Expression to Expression
- Is Relational Operator
Example:
Start Visual Basic with new Standard EXE project, place a CommandButton on a Form and write following code in Click event of the CommandButton.
Private Sub Command1_Click ()
Dim choice
As Integer
Choice
= 7
Select
Case Choice
Case 1,
2, 3
Print “Choice is in Range 1…3”
Case
Print “Choice is in Range 4…6”
Case
is = 7
Print “ Choice is 7”
Case 8,
9, is > 100
Print “Choice is 8,9 or greater then 100”
Case
Else
Print “Invalid Choice”
End Select
End Sub
|
Output:
Choice is 7
|
Press F5 key to run this project and Click the CommandButton. Output will be as shown above.
Example:
Start Visual Basic with new Standard EXE project, place a CommandButton on a Form and write following code in Click event of the CommandButton.
Private Sub Command1_Click ()
Dim Name
As Integer
Name = “Waqar”
Select
Case Choice
Case “Khalid”
, “Zeshan”
Print “Choice Name is Khalid or Zeshan”
Case Arsalan
to Fezan
Print “Choice Name starts with A, B,
C, D, E or F”
Case
is > “Tanvir”
Print “ Choice Name greater than character T”
Case
Else
Print “Invalid
Choice”
End Select
End Sub
|
Press F5 key to run this project and Click the CommandButton. The following output will be displayed on the Form.
Choice Name greater than character T
Example:
Start Visual Basic with new Standard EXE project, place a CommandButton on a Form and write following code in Click event of the CommandButton.
Private Sub Command1_Click ()
Dim dob
As Date
dob =
#03/11/2014#
Select
Case dob
Case #04/04/98#
, #03/02/98#
Print “Date of Birth is
Case #01/01/99#
, #012/031/99#
Print “Date of Birth is in the range
Case is
> #01/01/2000#
Print “Date of Birth is greater than01/01/2000
”
Case
Else
Print “Invalid
Date of Birth”
End Select
End Sub
|
Press F5 key to run this project and Click the CommandButton. The following output will be displayed on the Form.
0 comments:
Post a Comment