Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf.

Similar presentations


Presentation on theme: "Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf."— Presentation transcript:

1 Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 1 Chapter 4: The Selection Process in Visual Basic

2 Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 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 statements used in Visual Basic to make decisions. Understand the various comparison operators used in implementing decisions in Visual Basic. Use the If-Then-Else, If-Then-ElseIf, and Case decision structures. Use the list box control to select from a list of alternatives. 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.

3 Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 3 The Selection Process One of the key operations of a computer is to select between two or more alternatives to make a decision. Every decision involves a comparison between a variable and a constant, variable, or expression using logical operators. Decisions can involve two-alternatives or multiple alternatives.

4 Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 4 The If-Then-Else Decision Structure For two alternative decisions, the If-Then- Else decision structure should be used In pseudocode, this is: If condition is true then implement true alternative Else implement false alternative End Decision

5 Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 5 Multiple Alternatives For multiple alternatives, the general form in pseudocode is: Select one: Condition 1 is true; implement alternative 1 Condition 2 is true: implement alternative 2 Condition 3 is true; implement alternative 3 End Selection.

6 Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 6 The Two Alternative Decision Structure The If-Then-Else statement is: If condition is true Then statements for true alternative Else statements for false alternative End if The If-Then condition test expression1 comparison operator test expression2 where comparison operator is one of these six operators: Equal to: = Not equal to: <> Less then: < Greater than: > Less than or equal to: <= Greater than or equal to: >=

7 Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 7 The If-Then-Else Decision Structure (cont.)

8 Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 8 Example of If-Then-Else Decision to Compute Payroll sngPayRate = CCur(txtPayRate.text) intHours = CSng(txtHours.text) If intHours >= 40 Then curPay = sngPayRate * 40 + 1.5 * _ sngPayRate * (intHours - 40) Else curPay = intHours * sngPayRate Endif txtPay.Text = Format(curPay, _ ”currency”)

9 Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 9 One-Alternative Decision If there is only a true alternative, then this is a special case of the two-alternative decision structure If condition is true Then true alternative is implemented End If One-alternative decision can be combined with InputBox used to validate user input, e.g., to test that Customer Name textbox has something in it: If txtCustName.Text = “” then txtCustName.Text = InputBox(“Enter _ Name and try again”) Exit Sub End If Where the Exit Sub statement exits the event procedure

10 Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 10 If-Then-ElseIf Decision Structure One way to implement a multiple alter- native decision structure is through the If- Then-ElseIf decision structure: If condition1 true Then first set of statements ElseIf condition2 true Then second set of statements ElseIf condition3 true Then third set of statements Else last set of statements End If

11 Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 11 If-Then-ElseIf Decision Structure (Assume condition3 is True)

12 Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 12 Example of If-Then-ElseIf for Letter Grade Determination Dim intAverage as Integer Dim strLetterGrade as String intAverage = CInt(txtAverage.Text) If intAverage >= 90 then strLetterGrade = “A” ElseIf Average >= 80 then strLetterGrade = “B” ElseIf Average >= 70 then strLetterGrade = “C” ElseIf Average >= 60 then strLetterGrade = “D” Else strLetterGrade = “F” End if. txtLetter.Text = strLetterGrade

13 Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 13 Using the Select Case Decision Structure for Multiple Alternatives General form: Select Case expression Case Condition1 is true First set of statements Case Condition2 is true Second set of statements Case Condition3 is true Third set of statements Case Else Last set of statements End Select

14 Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 14 Case Conditions Conditions for Case statement can be in 3 forms: Test ConditionExample Value or expressionCase 91, 92, 93 Range of valuesCase 90 to 100 Comparison conditionCase Is > 89

15 Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 15 Example of Select Case to Determine LetterGrade 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

16 Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 16 Using the List Box The List box enables the user to select from a list of items. lst is the prefix for name and the List property of the list box can be set at design time or run time. The Text property of the list box is equal to the selected item. We can test the Text property to determine which item was selected.

17 Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 17 More Complex Decisions Decisions within decisions are know as nested decisions Interior decision must be an alternative of outer decision Decisions that combine two or more test conditions using logical operators are known as compound decisions And, Or, Not, and Xor are logical operators Both conditions must be able to stand alone

18 Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 18 Example of Nested Decisions Need to check if employee is hourly before checking for overtime: If strPayType = “Hourly” then If intHours > 40 Then curPay = 40 * sngPayRate + 1.5 + _ (intHours-40) * sngPayRate Else curPay = intHours * sngPayRate End If Else curPay = 40 * sngPayRate End If

19 Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 19 Example of Compound Decisions Using compound condition to test for average AND number of absences If sngAverage >= 90 AND intAbsences < 3 then strLetterGrade = “A” ElseIf sngAverage >= 80 AND intAbsences < 5 then strLetterGrade = “B” etc. In this case, if a student has an average of 93 and 4 absences, he/she will receive a grade of “B” because he/she has more than 3 absences.

20 Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 20 Example of Using List Box Dim strVideoType as String Dim curPrice as Currency strVideoType = lstTypes.Text Select Case strVideoType Case “Kids” curPrice = 0.99 Case “Regular” curPrice = 1.99 Case “Classic” curPrice = 2.99 End Select txtVideoType.Text = Format(curPrice, _ ”currency”) End Sub

21 Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 21 Using the Form_Load Event The Form_Load event occurs when the project is started and the form is loaded. Code from a command button can be cut (or copied) and then pasted into the form_load event

22 Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 22 Using the Debug ToolBar To view the Debug Toolbar, Select View|Toolbars and click the Debug checkbox The Debug Toolbar Run Stop Step Into Step Out Immediate Window Quick Watch Break Toggle Step Over Locals Watch Window Call Stack Breakpoint Window

23 Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf Select Case Complex Decision Form_Load Event Debugging Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 23 Debugging With the Immediate Window By clicking the Immediate Window icon on the debug toolbar, you can then print the current value of a textbox or variable Use the Print variable or textbox command to display its current value This can enable you to find errors in the code


Download ppt "Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf."

Similar presentations


Ads by Google