230 likes | 387 Views
Chapter 4: The Selection Process in Visual Basic. 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.
E N D
Chapter 4: The Selection Process in Visual Basic Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
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. Learning Objectives Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
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. The Selection Process Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
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 The If-Then-Else Decision Structure Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
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. Multiple Alternatives Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
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: The Two Alternative Decision Structure Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
The If-Then-Else Decision Structure (cont.) Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
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”) Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
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 One-Alternative Decision Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
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 If-Then-ElseIf Decision Structure Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
If-Then-ElseIf Decision Structure(Assume condition3 is True) Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
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 Example of If-Then-ElseIf for Letter Grade Determination Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
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 Using the Select Case Decision Structure for Multiple Alternatives Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
Conditions for Case statement can be in 3 forms: Test ConditionExample Value or expression Case 91, 92, 93 Range of values Case 90 to 100 Comparison condition Case Is > 89 Case Conditions Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
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 Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
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. Using the List Box Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
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 More Complex Decisions Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
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 Example of Nested Decisions Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
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. Example of Compound Decisions Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
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 Example of Using List Box Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
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 Using the Form_Load Event Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
To view the Debug Toolbar, Select View|Toolbars and click the Debug checkbox The Debug Toolbar Using the Debug ToolBar Run Stop Step Into Step Out Immediate Window Quick Watch Break Toggle Step Over Locals Watch Window Call Stack Breakpoint Window Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
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 Debugging With the Immediate Window Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy