4-1 chapter 4 the selection process in vb.net. 4-2 learning objectives understand the importance of...
TRANSCRIPT
4-1
Chapter 4
The Selection Process in VB .NET
4-2
Learning Objectives
Understand the importance of the selection process in programming.
Describe the various types of decisions that can be made in a computer program.
Discuss the statement forms used in VB .NET to make decisions.
Use the list box control to select from a list of alternatives.
4-3
Learning Objectives (continued)
Work with complex comparison structures and nested decisions to handle more sophisticated selection processes.
Use the scroll bar to input integer values. Use the Form_Load event to execute a
procedure when a form is loaded. Work with the debugging toolbar to find
program errors.
4-4
The Selection process
Do a comparison Result of comparison has a boolean value
– If True, execute one set of statements– If False, execute another set of statements
Example: If x=y Then
‘one set of statementsElse
‘another set of statementsEnd If
4-5
Types of decisions
Binary decision, only two alternatives– Yes – No– True – False
Multiple alternative– Choice is based on value of a certain variableSelect MyVariable
Condition 1: execute code for choice 1 Condition 2: execute code for choice 2
Condition 3: execute code for choice 3 End Selection
4-6
The Two Alternative Decision
Simple IfIf condition Then
‘Run code corresponding to true conditionEnd If
If-Then-ElseIf condition Then
‘Run code corresponding to true conditionElse
‘Run code corresponding to false conditionEnd If
4-7
Comparison Operators
4-8
Example: Find pay based on hours of work
If Hours > 40 Then
Pay = 40*PayRate+1.5*(Hours-40)*Payrate
Else
Pay = Hours*Payrate
End If
4-9
Step-by-Step 4-1: Creating a Payroll Project
Demo
4-10
Step-by-Step 4-2:Continuing Vintage DVDs Project
Demo
4-11
Multiple Alternative Decision:Compounded If’s
If condition1 Then‘Run code corresponding to condition1
ElseIf condition2 Then‘Run code corresponding to condition2
ElseIf condition3 Then‘Run code corresponding to condition3
Else‘Run code not corresponding to any of ‘the previous conditions
End If
4-12
Example: Calculate Grade
Private Sub btnLetter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles
btnLetter.Click
Dim intAverage As Integer, strLetterGrade As String intAverage = CInt(txtAverage.Text)
If intAverage >= 90 Then strLetterGrade = "A"
ElseIf intAverage >= 80 Then strLetterGrade = "B"
ElseIf intAverage >= 70 Then strLetterGrade = "C"
ElseIf intAverage >= 60 Then strLetterGrade = "D"
Else strLetterGrade = "F"
End if
txtLetter.Text = strLetterGrade End Sub
4-13
Step-by-Step 4-3:Creating the Letter Grade Project
Demo
4-14
Multiple Alternative Decision:The Select Case Decision Structure
Select Case expressionCase condition1
‘Code for condition1Case condition2
‘Code for condition2Case condition3
‘Code for condition 3Case Else
‘Code for other conditionsEnd Select
4-15
Comparison conditions
4-16
Example: Calculate Grade
Private Sub btnLetter_Click(ByVal sender As System.Object,_ ByVal e As System.EventArgs) Handles btnLetter.Click
Dim intAverage As Integer Dim strLetterGrade As String intAverage = CInt(txtAverage.Text) Select Case intAverage Case Is >= 90
strLetterGrade = "A" Case Is >= 80
strLetterGrade = "B" Case Is >= 70
strLetterGrade = "C" Case Is >= 60
strLetterGrade = "D" Case Else
strLetterGrade = "F" End Select
txtLetter.Text = strLetterGrade
End Sub
4-17
Step-by-Step 4-4:Revisiting Vintage DVDs Project
Demo
4-18
Application to Vintage DVD’s
Three types of DVD’s– Kids : $0.99– Regular : $1.99– Classic : $ 2.99
Feature to add: Indicate type of DVD and compute the price automatically
Need a way to enter DVD type
4-19
Application to Vintage DVD’sHow to input the DVD type
Naive – Use InputBox– Input errors are very likely
Smarter – Supply user with a “list” to choose from– Less input error– More guidance for the user
4-20
The ListBox Control
Like an extended TextBox Displays multiple text entries in a list Properties
– Items : the list of “items” displayed– SelectedItem : the item being selected
“Main Event”– SelectedIndexChanged : fires when user changes
the selection
4-21
Application to Vintage DVD’sCode
Code for lstTypes SelectedIndex-Changed event
Private Sub lstTypes_SelectedIndexChanged(ByVal sender As _ System.Object, ByVal e As System.EventArgs) Handles _ lstTypes.SelectedIndexChanged Dim strDVDType As String, decPrice As Decimal strDVDType = lstTypes.SelectedItem Select Case strDVDType Case "Kids"
decPrice = 0.99 Case "Regular"
decPrice = 1.99 Case "Classic"
decPrice = 2.99 End Select txtDVDPrice.Text = Format(decPrice, "c")
End Sub
4-22
More Complex Decisions
Nested Decisions Compound Decisions
4-23
Nested Decisions
When one of the decision branch contains another decision structure
Nested decision structure must be “nested”, i.e. the “sub” decision must be completely contained inside of the outer decision branch
No overlap of decision structures
4-24
Compound Decisions
When the condition contains more than one expression (comparison)– If This And That Then …..– If This Or That Then …..– If This And Not That Then …..
4-25
Logical Operators in Compound Decisions
4-26
The Form Load Event
Occurs when the form is loaded Ideal place for setting up initial conditions
4-27
Step-by-Step 4-5:Using the frmVintage_Load Event
Demo
4-28
VB .NET Debugging Tools
The Debug Toolbar
4-29
VB .NET Debugging Tools
Breakpoints
4-30
Copyright 2004 John Wiley & Sons, Inc.
All rights reserved. Reproduction or translation of this work beyond that permitted in section 117 of the 1976 United States Copyright Act without express permission of the copyright owner is unlawful. Request for further information should be addressed to the Permissions Department, John Wiley & Sons, Inc. The purchaser may make back-up copies for his/her own use only and not for distribution or resale. The Publisher assumes no responsibility for errors, omissions, or damages caused by the use of these programs or from the use of the information herein