The Selection Control Structure
The Selection Control Structure
Control Structures I
Ghana Technology University College
Lecturer – Dr. Forgor Lempogo
2021
Objectives
At the end of this lesson, students should be able to:
Make decisions using If…Then statements
Make decisions using If…Then…Else statements
Make decisions using nested If statements
Make decisions using Case statements
Use the GroupBox object
Use RadioButton objects in applications
Use message boxes to display dialogue
3
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Basic Program Control Structure
Condition:
The decision to be made
Results in a Boolean (True or False) answer
If <condition> Then
(condition is true - do all of the actions
coded in this branch)
End If
massageLabel.Text = CStr(paidAmount)
End If
If <condition> Then
(condition is true - do all of the actions
coded in this branch)
Else
(condition is false - do all of the actions
coded in this branch)
End If
10
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
If Statement with Else Branch
If totalAmount < 1000 Then
Else
End If
If <condition1> Then
(condition1 is true - do all of the actions
coded in this branch)
ElseIf <condition2> Then
(condition1 is false, condition2 is true - do
all of the actions coded in this branch)
Else
(condition1 is false, condition2 is false - do all
of the actions coded in this branch)
End If
12
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
The If – ElseIf Structure – Example 1
If totalAmount < 1000 Then
Else
End If
16
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Block If Example
Control Property Setting
Form Name frmStatus
Text Profit/Loss
Label 1 Name lblCosts
Text Costs:
label 2 Name lblRev
Text Revenue:
textbox1 name txtCosts
Textbox2 Name txtRev
Textbox3 Name txtResult
ReadOnly True
Button 1 Text Show Financial Status
End If
18
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Data Type Identification
IsDate(expression)
IsArray(variable)
IsDBNull(expression)
array.
If the variable names has been defined as
Dim names(100)
IsArray(names)
AND
OR
NOT
OrElse
AndAlso
Data validation:
Process of verifying that the input
data is within the expected range
27
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Input Data Validation
Example business rules include:
Missing Data Test
data values cannot be missing – this means a
TextBox control cannot be empty.
Numeric Data Test
numeric values must be tested to ensure they
are numeric.
Reasonableness Test
values (usually numeric) must fall within a
specified reasonable range.
28 CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Using the ANDAlso Operator - Example 1
'Calculate and display a gross pay
Double.TryParse(hoursTextBox.Text, hoursWorked)
grossLabel.Text = "Error"
End If
29
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Using the OrElse Operator - Example 2
'Calculate and display a gross pay
Double.TryParse(hoursTextBox.Text, hoursWorked)
Primary decision:
decision made by the outer selection structure
Secondary decision:
decision made by the inner selection structure
31
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Using the OrElse Operator - Example 2
Const VoteMsg As String = "You can vote."
Const RegisterMsg As String = "You need to register before you can vote."
Const TooYoungMsg As string = "You are too young to Vote."
Dim Age as Integer
Dim registered As String = String.Empty
Integer.TryParse(ageTextBox.Text, age)
registered = registeredTextBox.Text.ToUpper
If age >= 18 Then
if registered = "Y" Then
messageLabel.Text = VoteMsg
Else
messageLabel.Text = RegisterMsg
End If
Else
messageLabel.Text = TooYoungMsg
End If
32
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
The Case Selection Structure
Case valueList
A range
Case 1 To 3
Case Is >= 4
37
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Specifying a Range of Values
in an Expression List
To and Is keywords: specify a range of values in
a Case clause’s expression list
To:
When you know both the upper and lower
bounds of the range
Is:
When you know only one end of the range
Used with a comparison operator
38
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Select Case Blocks
Dim numberOrdered As Integer
Dim ItemPrice As Integer
Integer.TryParse(txtOrder.Text, numberOrdered)
Select Case numberOrdered
Case 1 To 5
itemPrice = 25
Case 1 To 10
itemPrice = 23
Case Is > 10
itemPrice = 20
Case Else
itemPrice = 0
End Select
39 txtResult.Text= itemPrice.ToString("C2")
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Select Case Blocks - Example
The following program converts the finishing position
in the Accra Milo Marathon into a descriptive phrase.
40
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Select Case Blocks - Example
Control Property Setting
Form Name frmRace
Text Accra Milo Marathon
Label Name lblPosition
AutoSize False
Text Finishing position (1, 2, 3,...):
Name btnEvaluate
41
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Select Case Blocks - Example
Dim position As Integer
Integer.Tryparse(txtPosition.Text, position)
Select Case position
Case 1
txtOutcome.Text = “Money in a Big Bag"
Case 2
txtOutcome.Text = “Money in a Small Bag"
Case 3
txtOutcome.Text = “Money in a very tiny Bag "
Case 4, 5
txtOutcome.Text = " Some Milo for breakfast."
Case Else
txtOutcome.Text = “Stories for your grand children."
42
End Select
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Using Radio Buttons
RadioButton control:
Allows the user to select only one of a group of
two or more choices
RadioButton choices are related but mutually
exclusive; only one can be selected
Container control:
Isolates a group of radio buttons
Includes GroupBox, Panel, and TableLayout
controls
43
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Using Radio Buttons (2)
Togo
Nigeria
Benin
Burkina Faso
44
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Using Radio Buttons (3)
Minimum number of radio buttons in a group is two
Must select a radio button to deselect another
48
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
The MessageBox.Show Method
The Syntax
MessageBox.Show(Text, Caption, Button, Icon, DefaultButton)
The Argument
Text
Text to display in the message box
Use sentence capitalization
Caption
Text (usually the application's name) to display in
the title bar of the message box
use book title capitalization.
49
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Buttons
Buttons to display in the message box.
50
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Icon
MessageBoxDefaultButton.Button1
default setting
MessageBoxDefaultButton.Button2
MessageBoxDefaultButton.Button3
53
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
User Returned Values
When a user select an option from a Message box, the message
box returns one of the following to the program:
Windows.Forms.DialogResult.OK
User chose the OK button
Windows.Forms.DialogResult.Cancel
User chose the CANCEL button
Windows.Forms.DialogResult.Abort
User chose the ABORT button
Windows.Forms.DialogResult.Retry
User chose the RETRY button
Windows.Forms.DialogResult.Ignore
User chose the IGNORE button
Windows.Forms.DialogResult.Yes
User chose the YES button
Windows.Forms.DialogResult.No
User chose the NO button
End IF
Me.Close()
End IF
e.Cancel = True
End IF
57
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Example 3
Dim Chosenbtn As DialogResult
Else
Instructions to continue the installation process
End IF
58
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Any Questions?