Chapter 5: More on the Selection Structure

Slides:



Advertisements
Similar presentations
Chapter 6: The Repetition Structure
Advertisements

Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic th Edition
1.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Chapter 11: Classes and Objects
Programming with Microsoft Visual Basic th Edition
Microsoft Visual Basic: Reloaded Chapter Five More on the Selection Structure.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 9 Decisions, Decisions, Decisions.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
An Introduction to Programming with C++ Fifth Edition Chapter 6 More on the Selection Structure.
Chapter 6: More on the Selection Structure
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Chapter 8: String Manipulation
Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.
Chapter 3: Using Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Chapter Four The Selection Structure
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
Chapter 4 The If…Then Statement
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Five More on the Selection Structure.
Programming with Microsoft Visual Basic 2008 Fourth Edition
Chapter 12: How Long Can This Go On?
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Seven More on the Repetition Structure.
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 4: The Selection Structure
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Five More on the Selection Structure.
Chapter 6: The Repetition Structure
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Programming with Microsoft Visual Basic th Edition
1 Week 5 More on the Selection Structure. 2 Nested, If/ElseIf/Else, and Case Selection Structures Lesson A Objectives After completing this lesson, you.
Chapter Four The Selection Structure Programming with Microsoft Visual Basic th Edition.
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
Programming with Microsoft Visual Basic th Edition
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
Controlling Program Flow with Decision Structures.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
Chapter 10 So Many Paths … So Little Time (Multiple-Alternative Selection Structures) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Chapter Five More on the Selection Structure Programming with Microsoft Visual Basic th Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Programming with Microsoft Visual Basic 2012 Chapter 3: Using Variables and Constants.
An Introduction to Programming with C++1 The Selection Structure Tutorial 6.
Course ILT Using complex selection structures Unit objectives Include radio buttons and check boxes in an interface, create and call a user- defined Sub.
An Introduction to Programming with C++1 More on the Selection Structure Tutorial 7.
Chapter 4: Decisions and Conditions
Chapter 4: Decisions and Conditions
More on the Selection Structure
The Selection Structure
Chapter 4: The Selection Structure
Programming with Microsoft Visual Basic 2008 Fourth Edition
Microsoft Visual Basic 2005 BASICS
Making Decisions in a Program
Objectives After studying this chapter, you should be able to:
CIS 16 Application Development Programming with Visual Basic
Microsoft Visual Basic 2005: Reloaded Second Edition
The Selection Structure
Presentation transcript:

Chapter 5: More on the Selection Structure

Previewing the Modified Covington Resort Application Figure 5-1 Interface showing the calculated amounts Figure 5-2 Recalculated amounts shown in the interface Programming with Microsoft Visual Basic 2012

Lesson A Objectives After studying Lesson A, you should be able to: Include a nested selection structure in pseudocode and in a flowchart Code a nested selection structure Desk-check an algorithm Recognize common logic errors in selection structures Include a multiple-alternative selection structure in pseudocode and in a flowchart Code a multiple-alternative selection structure Programming with Microsoft Visual Basic 2012

Nested Selection Structures When either a selection structure’s true path or its false path contains another selection structure The inner selection is called a nested selection Figure 5-4 A problem that requires a nested selection structure Programming with Microsoft Visual Basic 2012

Nested Selection Structures (cont.) Figure 5-5 A problem that requires two nested selection structures Programming with Microsoft Visual Basic 2012

Flowcharting a Nested Selection Structure Figure 5-6 Problem specification and a correct solution for the voter eligibility problem Programming with Microsoft Visual Basic 2012

Flowcharting a Nested Selection Structure (cont.) Figure 5-7 Another correct solution for the voter eligibility problem Programming with Microsoft Visual Basic 2012

Coding a Nested Selection Structure Figure 5-8 Code for the flowcharts in Figures 5-6 and 5-7 Programming with Microsoft Visual Basic 2012

Logic Errors in Selection Structures Four common errors: Using a compound condition rather than a nested selection structure Reversing the decisions in the outer and nested selection structures Using an unnecessary nested selection structure Including an unnecessary comparison in a condition Programming with Microsoft Visual Basic 2012

Logic Errors in Selection Structures (cont.) Algorithm The step-by-step instructions for accomplishing a task Desk-checking The process of reviewing the algorithm while seated at your desk rather than in front of a computer Also called hand-tracing You use a pencil and paper to follow the algorithm’s instructions by hand Desk-check to make sure you do not miss any instructions, and that existing instructions are correct and in the proper order Programming with Microsoft Visual Basic 2012

Logic Errors in Selection Structures (cont.) Figure 5-11 Sample data and expected results for the algorithm shown in Figure 5-10 Figure 5-10 A correct algorithm for the golf fee procedure Programming with Microsoft Visual Basic 2012

Logic Errors in Selection Structures (cont.) First Logic Error: Using a Compound Condition Rather Than a Nested Selection Structure Figure 5-12 Correct algorithm and an incorrect algorithm containing the first logic error Programming with Microsoft Visual Basic 2012

Logic Errors in Selection Structures (cont.) First Logic Error: Using a Compound Condition Rather Than a Nested Selection Structure (cont.) Figure 5-12 Correct algorithm and an incorrect algorithm containing the first logic error Figure 5-13 Results of desk-checking the incorrect algorithm from Figure 5-12 Programming with Microsoft Visual Basic 2012

Logic Errors in Selection Structures (cont.) Second Logic Error: Reversing the Outer and Nested Decisions Figure 5-14 Correct algorithm and an incorrect algorithm containing the second logic error Figure 5-15 Results of desk-checking the incorrect algorithm from Figure 5-14 Programming with Microsoft Visual Basic 2012

Logic Errors in Selection Structures (cont.) Third Logic Error: Using an Unnecessary Nested Selection Structure Figure 5-16 Correct algorithm and an inefficient algorithm containing the third logic error Figure 5-17 Results of desk-checking the inefficient algorithm from Figure 5-16 Programming with Microsoft Visual Basic 2012

Logic Errors in Selection Structures (cont.) Fourth Logic Error: Including an Unnecessary Comparison in a Condition Figure 5-18 Problem specification, a correct algorithm, and an inefficient algorithm Figure 5-19 Results of desk-checking the algorithms from Figure 5-18 Programming with Microsoft Visual Basic 2012

Multiple-Alternative Selection Structures When you need to choose from several different options, use a multiple-alternative selection structure Also called an extended selection structure Figure 5-20 Problem specification for the Allen High School problem Programming with Microsoft Visual Basic 2012

Multiple-Alternative Selection Structures (cont.) Figure 5-21 Pseudocode and flowchart containing a multiple-alternative selection structure Programming with Microsoft Visual Basic 2012

Multiple-Alternative Selection Structures (cont.) Figure 5-23 Excellent message shown in the interface Figure 5-22 Two versions of the code containing a multiple-alternative selection structure Programming with Microsoft Visual Basic 2012

The Select Case Statement Used when a multiple-alternative selection structure has many paths from which to choose It’s simpler and clearer to code the selection structure rather than several If…Then…Else statements Programming with Microsoft Visual Basic 2012

The Select Case Statement (cont.) Figure 5-24 Syntax and an example of the Select Case statement Programming with Microsoft Visual Basic 2012

The Select Case Statement (cont.) Specifying a Range of Values in a Case Clause Figure 5-25 Syntax and an example of specifying a range of values Programming with Microsoft Visual Basic 2012

Lesson A Summary To create a selection structure that evaluates both a primary and a secondary decision: Place (nest) the secondary decision’s selection structure within either the true or false path of the primary decision’s selection structure To verify that an algorithm works correctly: Desk-check (hand-trace) the algorithm Programming with Microsoft Visual Basic 2012

Lesson A Summary (cont.) To code a multiple-alternative selection structure: Use either If…Then…Else statements or the Select Case statement To specify a range of values in a Select Case statement’s Case clause: Use the To keyword when you know both the upper and lower values in the range Use the Is keyword when you know only one end of the range The Is keyword is used in combination with one of the following comparison operators: =, <, <=, >, >=, <> Programming with Microsoft Visual Basic 2012

Lesson B Objectives After studying Lesson B, you should be able to: Include a group of radio buttons in an interface Designate a default radio button Include a check box in an interface Compare Boolean values Programming with Microsoft Visual Basic 2012

Modifying the Covington Resort Application Figure 5-36 Revised TOE chart for the Covington Resort application (continues) Programming with Microsoft Visual Basic 2012

Modifying the Covington Resort Application (cont.) (continued) Figure 5-36 Revised TOE chart for the Covington Resort application Programming with Microsoft Visual Basic 2012

Modifying the Covington Resort Application (cont.) Figure 5-37 Partially completed interface for the Covington Resort application Programming with Microsoft Visual Basic 2012

Modifying the Covington Resort Application (cont.) Adding a Radio Button to the Interface Radio button Limits the user to only one choice from a group of two or more related but mutually exclusive choices The three-character ID for a radio button’s name is rad Figure 5-38 Atrium radio button added to the View group box Programming with Microsoft Visual Basic 2012

Modifying the Covington Resort Application (cont.) Adding a Radio Button to the Interface (cont.) The automatically selected radio button is called the default radio button Represents the user’s most likely choice The first button in the group Checked property Set to True to designate the default radio button Programming with Microsoft Visual Basic 2012

Modifying the Covington Resort Application (cont.) Adding a Check Box to the Interface Unlike radio buttons, check boxes provide one or more independent and nonexclusive items from which the user can choose Any number of check boxes on a form can be selected at the same time The three-character ID for a check box’s name is chk Figure 5-39 Vehicle parking fee check box added to the interface Programming with Microsoft Visual Basic 2012

Modifying the Covington Resort Application (cont.) Figure 5-40 Correct TabIndex values Programming with Microsoft Visual Basic 2012

Modifying the Calculate Button’s Code Figure 5-41 Modified pseudocode for the btnCalc_Click procedure Programming with Microsoft Visual Basic 2012

Modifying the Calculate Button’s Code (cont.) Figure 5-42 Modified list of named constants and variables Programming with Microsoft Visual Basic 2012

Modifying the Calculate Button’s Code (cont.) Figure 5-43 Named constants added to the procedure Figure 5-44 Variables added to the procedure Programming with Microsoft Visual Basic 2012

Modifying the Calculate Button’s Code (cont.) Comparing Boolean Values Figure 5-45 Examples of comparing Boolean values (continues) Programming with Microsoft Visual Basic 2012

Modifying the Calculate Button’s Code (cont.) (continued) Figure 5-45 Examples of comparing Boolean values Programming with Microsoft Visual Basic 2012

Modifying the ClearLabels Procedure CheckedChanged event Occurs when the value in a control’s Checked property changes Figure 5-50 ClearLabels procedure Programming with Microsoft Visual Basic 2012

Modifying the ClearLabels Procedure (cont.) Figure 5-51 Covington Resort application’s code at the end of Lesson B (continues) Programming with Microsoft Visual Basic 2012

Modifying the ClearLabels Procedure (cont.) (continued) Figure 5-51 Covington Resort application’s code at the end of Lesson B (continues) Programming with Microsoft Visual Basic 2012

Modifying the ClearLabels Procedure (cont.) (continued) Figure 5-51 Covington Resort application’s code at the end of Lesson B (continues) Programming with Microsoft Visual Basic 2012

Modifying the ClearLabels Procedure (cont.) (continued) Figure 5-51 Covington Resort application’s code at the end of Lesson B (continues) Programming with Microsoft Visual Basic 2012

Modifying the ClearLabels Procedure (cont.) (continued) Figure 5-51 Covington Resort application’s code at the end of Lesson B (continues) Programming with Microsoft Visual Basic 2012

Modifying the ClearLabels Procedure (cont.) (continued) Figure 5-51 Covington Resort application’s code at the end of Lesson B Programming with Microsoft Visual Basic 2012

Lesson B Summary To limit the user to only one choice in a group of two or more related but mutually exclusive choices: Use the RadioButton tool to add two or more radio buttons to the form To include two groups of radio buttons on a form, at least one of the groups must be placed within a container, such as a group box To allow the user to select any number of choices from a group of one or more independent and nonexclusive choices: Use the CheckBox tool to add one or more check box controls to the form Programming with Microsoft Visual Basic 2012

Lesson B Summary (cont.) To determine whether a radio button or check box is selected or unselected: Use the Checked property of the radio button or check box The property will contain the Boolean value True if the control is selected; otherwise, it will contain the Boolean value False To process code when the value in the Checked property of a radio button or check box changes: Enter the code in the radio button’s or check box’s CheckedChanged event procedure Programming with Microsoft Visual Basic 2012

Lesson C Objectives After studying Lesson C, you should be able to: Determine the success of the TryParse method Generate random numbers Show and hide a control while an application is running Programming with Microsoft Visual Basic 2012

Using the TryParse Method for Data Validation Converts a string to a number of a specific data type With successful conversion: The TryParse method stores the number in the variable specified in the method’s NumericVariableName argument With unsuccessful conversion: TryParse stores the number 0 in the variable Programming with Microsoft Visual Basic 2012

Using the TryParse Method for Data Validation (cont.) Figure 5-58 Syntax and an example of using the Boolean value returned by the TryParse method Programming with Microsoft Visual Basic 2012

Using the TryParse Method for Data Validation (cont.) Figure 5-59 Sample run of the original Click event procedure Programming with Microsoft Visual Basic 2012

Using the TryParse Method for Data Validation (cont.) Figure 5-60 Modified btnCalc_Click procedure Programming with Microsoft Visual Basic 2012

Generating Random Integers Pseudo-random number generator A device that produces a sequence of numbers that meet certain statistical requirements for randomness Create a Random object by declaring it in a Dim statement Use the Random.Next method to generate random integers Programming with Microsoft Visual Basic 2012

Generating Random Integers (cont.) Figure 5-61 Syntax and examples of generating random integers Programming with Microsoft Visual Basic 2012

Generating Random Integers (cont.) Figure 5-62 Roll ‘Em Game application’s interface Figure 5-63 Pseudocode for the Roll the Dice button’s Click event procedure Programming with Microsoft Visual Basic 2012

Generating Random Integers (cont.) Figure 5-64 Result of clicking the Roll the Dice button Programming with Microsoft Visual Basic 2012

Showing and Hiding a Control Hide objects by changing their Visible property from True to False Figure 5-65 Resized form Figure 5-66 Interface with six of the picture boxes hidden Programming with Microsoft Visual Basic 2012

Showing and Hiding a Control (cont.) Figure 5-67 Roll the Dice button’s Click event procedure Programming with Microsoft Visual Basic 2012

Lesson C Summary To determine whether the TryParse method converted a string to a number of the specified data type: Use the syntax booleanVariable = dataType.TryParse(string, numericVariableName) The TryParse method returns the Boolean value True when the string can be converted to the numeric dataType Otherwise, it returns the Boolean value False Programming with Microsoft Visual Basic 2012

Lesson C Summary (cont.) To generate random integers: Create a Random object to represent the pseudo-random number generator The syntax for creating a Random object is Dim randomObjectName As New Random Use the Random.Next method to generate a random integer The method’s syntax is randomObjectName.Next(minValue, maxValue) The Random.Next method returns an integer that is greater than or equal to minValue, but less than maxValue The Random.Next method’s return value is assigned to a variable Programming with Microsoft Visual Basic 2012

Lesson C Summary (cont.) To show or hide a control while an application is running: Set the control’s Visible property to the Boolean value True to show the control during runtime Set the control’s Visible property to the Boolean value False to hide the control during runtime Programming with Microsoft Visual Basic 2012