Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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

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

3 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-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 statements Else ‘another set of statements End If

5 4-5 Types of decisions Binary decision, only two alternatives – Yes – No – True – False Multiple alternative – Choice is based on value of a certain variable Select MyVariable Condition 1: execute code for choice 1 Condition 2: execute code for choice 2 Condition 3: execute code for choice 3 End Selection

6 4-6 The Two Alternative Decision Simple If If condition Then ‘Run code corresponding to true condition End If If-Then-Else If condition Then ‘Run code corresponding to true condition Else ‘Run code corresponding to false condition End If

7 4-7 Comparison Operators

8 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

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

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

11 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

12 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

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

14 4-14 Multiple Alternative Decision: The Select Case Decision Structure Select Case expression Case condition1 ‘Code for condition1 Case condition2 ‘Code for condition2 Case condition3 ‘Code for condition 3 Case Else ‘Code for other conditions End Select

15 4-15 Comparison conditions

16 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

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

18 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

19 4-19 Application to Vintage DVD’s How 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

20 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

21 4-21 Application to Vintage DVD’s Code 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

22 4-22 More Complex Decisions Nested Decisions Compound Decisions

23 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

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

25 4-25 Logical Operators in Compound Decisions

26 4-26 The Form Load Event Occurs when the form is loaded Ideal place for setting up initial conditions

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

28 4-28 VB.NET Debugging Tools The Debug Toolbar

29 4-29 VB.NET Debugging Tools Breakpoints

30 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


Download ppt "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."

Similar presentations


Ads by Google