Chapter 5 Specifying alternate courses of action Selection statements. Lecture Learning programming using Visual Basic Net – Chapter 5 Specifying alternate courses of action Selection statements Lecture Learning,Learning programming,Lecture Learning programming
CHAPTER FIVE Specifying Alternate Courses of Action: Selection Statements 5- 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- 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- 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.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: The condition The statements to perform for the first outcome The statements to perform for the second outcome McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 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 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 5- 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- 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- 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.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 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 526 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 527 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 528 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 529 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 530 ... Companies, Inc All rights reserved 51 3 5. 4 The MsgBox() Function (cont.) • MsgBox() Return Values – – – – – – – vbOK (1) vbCancel (2) vbAbort (3) vbRetry (4) vbIgnore (5) vbYes (6) vbNo (7) McGraw... reserved 51 2 5. 4 The MsgBox() Function (cont.) • MsgBox() Button Combinations – – – – – – MsgBoxStyle.AbortRetryIgnore (2) MsgBoxStyle.OKOnly (0) MsgBoxStyle.OKCancel (1) MsgBoxStyle.RetryCancel (5) ... reserved 5- 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)