CHAPTER FIVE Specifying Alternate Courses of Action: Selection Statements – Pemrograman Visual

advertisement
Matakuliah
Tahun
: T0063 – Pemrograman Visual
: 2009
Specifying Alternate Courses of Action:
Selection Statements
CHAPTER FIVE
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.
Bina Nusantara University
2
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.
Bina Nusantara University
3
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.
Bina Nusantara University
4
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.
Bina Nusantara University
5
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
Bina Nusantara University
6
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.
Bina Nusantara University
7
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
Bina Nusantara University
8
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)
Bina Nusantara University
9
5.2 The If…Then...Else Statement (cont.)
– If…Then
• Sometimes only one of two outcomes requires
processing.
– If condition Then
statementblock
– End If
Bina Nusantara University
10
5.3 Nested If Statements
• If statements may contain one within another.
•
If (X<Y) And (Y<Z) Then
AscOrDesc = “ascending”
Else
If (X>Y) And (Y>Z) Then
AscOrDesc = “descending”
Else
AscOrDesc = “neither ascending nor descending”
End If
End If
Bina Nusantara University
11
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
Bina Nusantara University
12
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.
Bina Nusantara University
13
5.5 The RadioButton Control (cont.)
– Properties
Appearance
CheckAlign
Checked
Enabled
FlatStyle
Bina Nusantara University
Image
ImageAlign
Text
TextAlign
Visible
14
5.5 The RadioButton Control (cont.)
– Events
• CheckChanged event.
– Occurs when the value of the Checked property
changes.
Bina Nusantara University
15
5.6 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.
Bina Nusantara University
16
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.
Bina Nusantara University
17
5.7 The CheckBox Control (cont.)
– Properties of the CheckBox control
• CheckAlign
• Checked
• CheckState
• Enabled
• Text
• TextAlign
• ThreeState
• Visible
Bina Nusantara University
18
5.8 The Exit Sub Statement
• For decisions where one of the appropriate actions is to
stop processing.
• Causes execution to skip directly to End Sub.
• Is not limited to If…Then…Else and Select Case
statements.
Bina Nusantara University
19
Download