1 / 23

Chapter 4: The Selection Process in Visual Basic

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.

apria
Download Presentation

Chapter 4: The Selection Process in Visual Basic

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4: The Selection Process in Visual Basic Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

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

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

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

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

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

  7. The If-Then-Else Decision Structure (cont.) 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”) Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

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

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

  11. If-Then-ElseIf Decision Structure(Assume condition3 is True) Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

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

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

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

  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 Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

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

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

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

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

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

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

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

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

More Related