4-1 chapter 4 the selection process in vb.net. 4-2 learning objectives understand the importance of...

30
4-1 Chapter 4 The Selection Process in VB .NET

Upload: austin-noah-oliver

Post on 17-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

4-1

Chapter 4

The Selection Process in VB .NET

Page 2: 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

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.

Page 3: 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

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.

Page 4: 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

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

Page 5: 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

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

Page 6: 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

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

Page 7: 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

4-7

Comparison Operators

Page 8: 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

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

Page 9: 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

4-9

Step-by-Step 4-1: Creating a Payroll Project

Demo

Page 10: 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

4-10

Step-by-Step 4-2:Continuing Vintage DVDs Project

Demo

Page 11: 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

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

Page 12: 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

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

Page 13: 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

4-13

Step-by-Step 4-3:Creating the Letter Grade Project

Demo

Page 14: 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

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

Page 15: 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

4-15

Comparison conditions

Page 16: 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

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

Page 17: 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

4-17

Step-by-Step 4-4:Revisiting Vintage DVDs Project

Demo

Page 18: 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

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

Page 19: 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

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

Page 20: 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

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

Page 21: 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

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

Page 22: 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

4-22

More Complex Decisions

Nested Decisions Compound Decisions

Page 23: 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

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

Page 24: 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

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 …..

Page 25: 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

4-25

Logical Operators in Compound Decisions

Page 26: 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

4-26

The Form Load Event

Occurs when the form is loaded Ideal place for setting up initial conditions

Page 27: 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

4-27

Step-by-Step 4-5:Using the frmVintage_Load Event

Demo

Page 28: 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

4-28

VB .NET Debugging Tools

The Debug Toolbar

Page 29: 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

4-29

VB .NET Debugging Tools

Breakpoints

Page 30: 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

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