Chapter 5: More on the Selection Structure - PowerPoint PPT Presentation

PPT – Chapter 5: More on the Selection Structure PowerPoint presentation | free to view - id: 224010-YTA3M

The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
Title:

Chapter 5: More on the Selection Structure

Description:

Using a logical operator when a nested selection structure is needed ... An algorithm is the set of step-by-step instructions that accomplish a task ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 47
Provided by: david2325
Category:
Tags:
Transcript and Presenter's Notes

Title: Chapter 5: More on the Selection Structure

1
Chapter 5 More on the Selection Structure
Programming with Microsoft Visual Basic .NET,
Second Edition
2
Nested, If/ElseIf/Else, and Case Selection
StructuresLesson A Objectives
• 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

3
Nested, If/ElseIf/Else, and Case Selection
StructuresLesson A Objectives (continued)
• Code an If/ElseIf/Else selection structure
• Include a Case selection structure in pseudocode
and in a flowchart
• Code a Case selection structure
• Write code that uses the Is, TypeOfIs, and Like
comparison operators

4
Nested Selection Structures
• When a selection structures true path or its
false path contains another selection structure,
the inner selection structure is referred to as a
nested selection structure

5
Nested Selection Structures (continued)
6
Nested Selection Structures (continued)
Part of Figure 5-3 Pseudocode showing the nested
selection structure in the true path
7
Nested Selection Structures (continued)
Part of Figure 5-5 Pseudocode showing the nested
selection structure in the false path
8
Logic Errors in Selection Structures
• Common logic errors with selection structures
• Using a logical operator when a nested selection
structure is needed
• Reversing the primary and secondary decisions
• Using an unnecessary nested selection structure

9
Logic Errors in Selection Structures (continued)
• An algorithm is the set of step-by-step
• Desk-checking means that you use sample data to
walk through each of the steps in the algorithm
manually, just as if you were the computer
• Desk-checking is also called hand-tracing

10
Logic Errors in Selection Structures (continued)
Figure 5-7 A correct algorithm for the vacation
procedure
11
Logic Errors in Selection Structures (continued)
Figure 5-8 Results of desk-checking the correct
algorithm shown in Figure 5-7
12
Using a Logical Operator Rather Than a Nested
Selection Structure
Figure 5-9 Correct algorithm and an incorrect
algorithm containing the first logic error
13
Reversing the Primary and Secondary Decisions
Figure 5-11 Correct algorithm and an incorrect
algorithm containing the second logic error
14
Using an Unnecessary Nested Selection Structure
Figure 5-13 Correct algorithm and an inefficient
algorithm containing the third logic error
15
The If/ElseIf/Else Form of the Selection Structure
• Figure 5-16 shows two versions of the Visual
Basic .NET code for the grade procedure

Figure 5-15 Letter grades and messages
16
The If/ElseIf/Else Form of the Selection
Structure (continued)
Figure 5-16 Two versions of the Visual Basic
.NET code for the grade procedure
17
The Case Form of the Selection Structure
Figure 5-18 Syntax and an example of the Select
Case statement
18
Figure 5-19 Results of desk-checking the grade
procedure shown in Figure 5-18
19
Using To and Is in an ExpressionList
• To keyword specifies a range of minimum and
maximum values
• Case 1 To 5
• Is keyword specifies a range of values when you
know only one value, either the minimum or the
maximum
• Case Is gt 10

20
The Is, TypeOfIs, and Like Comparison Operators
• The Is operator determines whether two object
references refer to the same object
• An object reference is a memory address within
the computers internal memory it indicates
where in memory the object is stored

21
The Is, TypeOfIs, and Like Comparison Operators
(continued)
• The TypeOfIs operator determines whether an
object is a specified type
• The Like operator uses pattern matching to
determine if one string is equal to another string

22
The Is Comparison Operator
• Use the Is operator to determine whether two
object references refer to the same object
• An object reference is a memory address within
the computers internal memory
• Syntax objectReference1 Is objectReference2
• The Is operator evaluates to True if
objectReference1 is the same as objectReference2
otherwise it evaluates to False

23
The TypeOfIs Comparison Operator
• Use the TypeOfIs operator to determine whether
an object is a specified type
• Syntax TypeOf object Is objectType
• The TypeOfIs operator evaluates to True if the
type of object is the same as objectType
otherwise it evaluates to False

24
The Like Comparison Operator
• The Like operator allows you to use pattern
matching to determine whether one string is equal
to another string
• Syntax string Like pattern
• Both string and pattern must be String
expressions
• pattern can contain one or more of the
pattern-matching characters
• The Like operator evaluates to True if string
matches pattern otherwise it evaluates to False

25
The Like Comparison Operator (continued)
Figure 5-24 Pattern-matching characters
26
The Math Practice ApplicationLesson B Objectives
• Include a group of radio buttons in an interface
• Designate a default radio button
• Include a check box in an interface
• Create a user-defined Sub procedure
• Generate random numbers using the Random object
and the Random.Next method

27
The Math Practice ApplicationLesson B Objectives
(continued)
• Call a user-defined Sub procedure
• Invoke a radio button controls Click event
procedure from code
• Process code when a form is first loaded into the
computers memory

28
Completing the User Interface
• The application should display the addition or
subtraction problem on the screen, then allow the
student to enter the answer, and then verify that
• If the students answer is not correct, the
application should give the student as many
chances as necessary to answer the problem
correctly

29
Completing the User Interface (continued)
Figure 5-28 Sketch of the Math Practice
applications user interface
30
• Limits the user to one choice in a group of
options
• Should have a unique access key, which allows the
user to select the button using the keyboard

31
• The selected button is called the default radio
button
• The selected button is either the radio button
that represents the users most likely choice or
the first radio button in the group
button by setting the buttons Checked property
to the Boolean value True

32
Adding a Check Box Control to the Form
• Checkbox controls used for adding a check box
control to the interface
• Use check box controls to allow the user to
select any number of choices from a group of one
or more independent and nonexclusive choices
• Any number of check boxes on a form can be
selected at the same time

33
Coding the Math Practice Application
• The Click event procedures for seven of the
controls and the Load event for the MathForm need
to be coded
• In this lesson, you code all but the Click event
procedures for the uiExitButton control and the
controls (which you code in Lesson C)

34
Creating a User-Defined Sub Procedure
• A user-defined Sub procedure is a collection of
code that can be invoked from one or more places
in an application
• Enter Private Sub SubName() just above the End
Class statement
• The End Sub will automatically be generated

35
Creating a User-Defined Sub Procedure (continued)
• The rules for naming a user-defined Sub procedure
are the same as for naming variables
• Sub procedure names should be entered using
Pascal case
• Capitalize the first letter in the name and the
first letter of each subsequent word in the name

36
Generating Random Numbers
• Visual Studio .NET provides a pseudo-random
number generator
• Produces a sequence of numbers that meet certain
statistical requirements for randomness
• Dim GeneratorRandom As New Random()
• Methods
• Next(minValue, maxValue)
• NextDouble()

37
• Use the Visual Basic .NET Call statement to call
(invoke) a user-defined Sub procedure
• Syntax Call procedurename(argumentlist)
• The square brackets in the syntax indicate that
the argumentlist is optional

38
the controls Click event procedure should
• Display the appropriate mathematical operator
(either a plus sign or a minus sign) in the
uiOperatorPictureBox control
• Generate and display two random numbers in the
uiNum1Label and uiNum2Label controls

39
Coding the Forms Load Event Procedure
• Instructions entered in the forms Load event
procedure are processed when the application is
started and the form is loaded into memory
• To automatically display an addition problem when
the Math Practice interface first appears, enter
one of the following statements in the MathForms
• Call GenerateAndDisplayNumbers()

40
Completing the Math Practice ApplicationLesson C
Objectives
• Select the existing text in a text box control
• Code a check box controls Click event procedure
• Display and hide a control

41
Procedure
• Need to code the Click event procedures for the
ox controls
• Figure 5-49 shows the pseudocode for the
procedure

42
Procedure (continued)
Figure 5-49 Pseudocode for the
procedure
43
Coding the uiSummaryCheckBox Click Event Procedure
• The uiSummaryCheckBox controls Click event
procedure is responsible for both displaying and
hiding the uiSummaryGroupBox control
• The procedure should
• Display the group box control when the user
selects the check box
• Hide the group box control when the user
deselects the check box

44
Summary
• To create a selection structure that evaluates
both a primary and a secondary decision, place
(nest) the selection structure for the secondary
decision within either the true path or false
path of the selection structure for the primary
decision
• To code a multiple-path (or extended) selection
structure, use either the IfThenElse statement
or the Select Case statement

45
Summary (continued)
• To limit the user to only one choice in a group
of two or more related and mutually exclusive
• To allow the user to select any number of choices
from a group of one or more independent and
nonexclusive choices, add a check box control
• To call (invoke) a user-defined Sub procedure,
use the Call statement

46
Summary (continued)
• To process code when the form is loaded into
memory, enter the code in the forms Load event
procedure
• To display or hide a control, set the controls
Visible property
• To code a check box controls Click event
procedure, use a selection structure to determine
whether the check box was either selected or
deselected by the user