CHAPTER FIVE
Specifying Alternate Courses of
Action: Selection Statements
5- 2
Introduction
• A need to select an appropriate action from several
alternatives.
• Two statements will make this possible:
– The If…Then…Else statement.
– The Select Case statement.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.
5- 3
Objectives
• Construct programs that select alternative actions.
• Compare the Select Case statement with the If…Then…
Else statement.
• Create GUIs using the MsgBox() function, Radio Button,
GroupBox, and CheckBox.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.
5- 4
5.1 The Decision-Making Process
• We must be very precise in writing the criterion and
alternative actions for decisions.
• In a program,
– A condition is represented as an expression.
– An outcome is the result of an evaluated condition.
– An appropriate action follows the outcome.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.
5- 5
5.2 The If…Then...Else Statement
•
•
The If…Then…Else statement enables a program to
handle situations having two outcomes.
The statement has three parts:
1. The condition.
2. The statements to perform for the first
outcome.
3. The statements to perform for the second
outcome.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.
5- 6
5.2 The If…Then...Else Statement (cont.)
• Syntax and Action of If…Then…Else
– The If…Then…Else statement has the following syntax:
• If condition Then
– Statementblock1
• Else
– Statementblock2
• End If
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.
5- 7
5.2 The If…Then...Else Statement (cont.)
– Run Time: The Effect of the If…Then…Else Statement
• True: the computer executes the statements in
statementblock1.
• False: the computer skips to Else and executes the
statements in statementblock2.
– Meta Statements
• Describe compound statement with a single phrase.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.
5- 8
5.2 The If…Then...Else Statement (cont.)
– Problem Solving and Pseudocode
• Problem solving is the process of writing code to
perform a required task.
• Pseudocode is an English-like outline of the logical for
program code.
– Ex. Request address from user
Receive input in a textbox
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.
5- 9
5.2 The If…Then...Else Statement (cont.)
– Using Logical Expressions in If…Then…Else Statements
• The condition of an If…Then…Else statement may be a
logical expression.
– Ex. (YearsExperience>5) And
(NumberOfLanguages>=3)
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.
5.2 The If…Then...Else Statement (cont.)
– If…Then
• Sometimes only one of two outcomes requires
processing.
– If condition Then
statementblock
– End If
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.
510
5.3 Nested If Statements
• If statements may contain one within another.
If (X
AscOrDesc = “ascending”
Else
If (X>Y) And (Y>Z) Then
AscOrDesc = “descending”
Else
AscOrDesc = “neither ascending nor descending”
End If
End If
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.
511
5.4 The MsgBox() Function
• Displays a message box on the screen and waits for the
user to click one of the button.
• Returns a value that indicates which button the user
clicked.
• Syntax:
MsgBox(message, mbStyle, title) As
MsgBoxResult
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.
512
5.4 The MsgBox() Function (cont.)
• MsgBox() Button Combinations
–
–
–
–
–
–
MsgBoxStyle.AbortRetryIgnore (2)
MsgBoxStyle.OKOnly (0)
MsgBoxStyle.OKCancel (1)
MsgBoxStyle.RetryCancel (5)
MsgBoxStyle.YesNo (4)
MsgBoxStyle.YesNoCancel (3)
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.
513
5.4 The MsgBox() Function (cont.)
• MsgBox() Return Values
–
–
–
–
–
–
–
vbOK (1)
vbCancel (2)
vbAbort (3)
vbRetry (4)
vbIgnore (5)
vbYes (6)
vbNo (7)
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.
514
5.5 The RadioButton Control
• Ensures that user will select only one option
– Appearance and Use
• Appears as descriptive text next to a circle.
• User selects with the mouse.
• Selection is transferred if user clicks another
radiobutton.
• Developers should limit the number of radiobuttons to
about seven per form.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.
515
5.5 The RadioButton Control (cont.)
– Properties
Appearance
CheckAlign
Checked
Enabled
FlatStyle
McGraw Hill/Irwin
Image
ImageAlign
Text
TextAlign
Visible
©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.
516
5.5 The RadioButton Control (cont.)
– Events
• CheckChanged event.
– Occurs when the value of the Checked property
changes.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.
517
5.6 The GroupBox Control
• Allows you to group RadioButtons to correspond to
categories of items.
– Appearance and Use
• Appears as a rectangle surrounding the controls it
groups together.
• Descriptive text can help identify the “group.”
• You may drag a RadioButton in or out of a GroupBox.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.
518
5.6 The GroupBox Control (cont.)
– Properties
• Enabled
• FlatStyle
• Text
• Visible
– Events
• Click
• CheckChanged
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.
519
5.7 The CheckBox Control
• Used when a combination of options may be selected.
– Appearance and Use
• Appears as descriptive text next to a square.
• User may select a CheckBox by clicking it with the
mouse.
• User may deselect a CheckBox by clicking it again with
the mouse.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.
520
5.7 The CheckBox Control (cont.)
– Properties and Events
• Similar to the RadioButton.
• CheckState property gets or sets the state of the
CheckBox.
• ThreeState property will allow the user to select an
Indeterminate state.
• CheckBox can respond to a CheckedChanged event.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.
521
5.7 The CheckBox Control (cont.)
– Properties of the CheckBox control
• CheckAlign
• Checked
• CheckState
• Enabled
• Text
• TextAlign
• ThreeState
• Visible
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.
522
5.8 The Select Case Statement
• Can handle conditions with multiple outcomes.
– Syntax and Action of Select Case
Select Case testexpression
Case expressionlist1
statementblock1
Case expressionlist2
statementblock2
…
Case expressionlistN
statementblockN
Case Else
statementblock
End Select
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.
523
5.8 The Select Case Statement (cont.)
– Run Time: The Effect of the Select Case Statement
• First the computer evaluates the test expression.
• Then it tries to match the resulting value.
• The search starts at the top expression list.
• The computer stops at the first match.
• The corresponding statement block is executed.
• Control resumes after the End Select.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.
524
5.8 The Select Case Statement (cont.)
• If no match occurs and a Case Else exists, then the Case
Else block is executed.
• Then control resumes after the End Select.
• If no match occurs and no Case Else exists, then control
resumes after the End Select.
– Ranges
• Two variations
– Use the keyword “To”
» Ex. 25 To 37
– Use the keyword “Is”
» Ex. Is >= 77
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.
525