Download presentation
Presentation is loading. Please wait.
Published byMaude Stanley Modified over 8 years ago
1
CHAPTER FIVE Specifying Alternate Courses of Action: Selection Statements
2
McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 2 Selection Statements VB Selection Statements –The If…Then…Else statement. –The Select Case statement. Using return value of the MsgBox function VB Selection Controls –Radio Button Defining a group with a container (Form or Group Box) –Check Box
3
McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 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 a logical expression. Meta Statements - Describe compound statement with a single phrase. –An outcome is the result of an evaluated condition. –An appropriate action follows the outcome.
4
McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 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 a true outcome. 3.The statements to perform for a false outcome.
5
McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 5 5.2 The If…Then...Else Statement (cont.) The If…Then…Else statement has the following syntax: –If condition Then True statement block –Else False statement block –End If If…Then –If condition Then statementblock –End If
6
McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 6 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
7
McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 7 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
8
McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 8 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
9
McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 9 5.4 The MsgBox() Function (con’t) MsgBox() Button Combinations –MsgBoxStyle.AbortRetryIgnore (2) –MsgBoxStyle.OKOnly (0) –MsgBoxStyle.OKCancel (1) –MsgBoxStyle.RetryCancel (5) –MsgBoxStyle.YesNo (4) –MsgBoxStyle.YesNoCancel (3) MsgBox() Return Values –vbOK (1) –vbCancel (2) –vbAbort (3) –vbRetry (4) –vbIgnore (5) –vbYes (6) –vbNo (7)
10
McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 10 5.5 The RadioButton Control Ensures that user will select only one option –Selection is transferred if user clicks another radiobutton. –Limit the number of radiobuttons to 7 per form. (use list or combo box for more items) Radio button
11
McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 11 5.5 The RadioButton Control (cont.) –Properties AppearanceImage CheckAlignImageAlign CheckedText EnabledTextAlign FlatStyleVisible –Events CheckChanged event. –Occurs when the value of the Checked property changes.
12
McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 12 5.6 The GroupBox Control Allows grouping of RadioButtons into categories of items Descriptive Text to identify the group Group Box
13
McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 13 5.6 The GroupBox Control (cont.) –Properties Enabled FlatStyle Text Visible –Events Click CheckChanged
14
McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 14 5.7 The CheckBox Control Used when a combination of options may be selected. Check Box
15
McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 15 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.
16
McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 16 5.7 The CheckBox Control (cont.) –Properties of the CheckBox control CheckAlign Checked CheckState Enabled Text TextAlign ThreeState Visible
17
McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 17 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
18
McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 18 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. 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.
19
McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 19 5.8 The Select Case Statement (cont.) –Ranges Two variations –Use the keyword “To” »Ex. 25 To 37 –Use the keyword “Is” »Ex. Is >= 77 –If versus Select Case If a decision has only two outcomes and the condition can be expressed as a single logical expression, then the If statement is easier to read. If a decision has multiple outcomes that depend on a single expression, then the Select Case is usually easier to read. If the outcomes depend on a number of conditions that may be independent, then embedded If statements are better.
20
McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 20 5.9 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.
21
McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 21 Quick Review What is evaluated to determine the path of execution in a selection statement? What language is used to describe the logical steps (algorithm) when designing a problem solution? Which selection statement enables a program to choose one of two actions based on a condition that evaluates to true or false?
22
McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 22 Quick Review (cont.) Which two controls enable the user to make choices with the GUI? Which control groups a set of choices of which only one can be selected? Which selection statement handles a single condition that may have more than one outcome? Which statement enables a program to stop execution before a procedure is completed?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.