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- 10 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. 5- 11 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 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 12 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. 5- 13 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. 5- 14 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. 5- 15 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. 5- 16 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. 5- 17 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. 5- 18 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. 5- 19 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. 5- 20 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. 5- 21 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. 5- 22 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. 5- 23 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. 5- 24 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. 5- 25 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. 5- 26 5.8 The Select Case Statement (cont.) – 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. McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 27 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. McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 28 Chapter Summary • To make a decision, we first evaluate a condition and determine its outcome. • Programs must imitate the decision-making process. • The logic of decision-making may be complex. McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 29 Chapter Summary (cont.) • Developers use pseudocode to describe the logical steps in a problem solution. • A meta statement describes a compound statement with a single phrase. • The If…Then…Else statement enables a program to choose one of two actions based on a condition. McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 30 Chapter Summary (cont.) • The RadioButton, GroupBox, and CheckBox are three controls that enable the user to make choices with the GUI. • The SelectCase statement handles a single condition that may have more than one outcome. • The Exit Sub statement enables a program to stop execution before a procedure is completed. McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
© Copyright 2025 Paperzz