1 / 38

Lecture 4: Decision and Selection

Lecture 4: Decision and Selection. Daniel Chen. The selection process in programming. Decision types and VB statements The If-Then-Else, If-Then-ElseIf, and Case decision structures. Use the scroll bar and list box control The complex comparison structures Use the Form_Load event

jacques
Download Presentation

Lecture 4: Decision and Selection

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. Lecture 4: Decision and Selection Daniel Chen

  2. The selection process in programming. Decision types and VB statements The If-Then-Else, If-Then-ElseIf, and Case decision structures. Use the scroll bar and list box control The complex comparison structures Use the Form_Load event Debugging issues Topics

  3. One of the key operations of a computer is to select between two or more alternatives to make a decision to real life problems. Regular time versus overtime 1500 free minutes – but I used 2000 Buy three get one free Insurance premium depends on your age The Selection Process

  4. Every decision involves a comparison between a variable and a constant, variable, or expression using logical operators. values being compared must be of same data type Decisions in VB

  5. Relational operator in VB

  6. two-alternatives multiple alternatives. Types of decisions

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

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

  9. The If-Then-Else Decision Structure (cont.)

  10. Payroll problem (overtime issue) If hoursworked > 40 then gross pay = 40*PayRate + 1.5 (Hours-40) * PayRate Else gross pay=hoursworked * payrate End decision Example of If-Then-Else Decision

  11. 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. One-Alternative Decision

  12. To test that EmployeeName textbox has something in it: If txtName.Text = “” then txtName.Text = InputBox(“Enter _ Name and try again”) Exit Sub End If Where the Exit Sub statement exits the event procedure IF-Then-End IF example

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

  14. If-Then-ElseIf Decision Structure(Assume condition3 is True)

  15. If average is 90 or higher then grade is A else if average is 80 or higher then grade is B else if average is 70 or higher then grade is C else if average is 60 or higher then grade is D else grade is F End Selection Example of If-Then-ElseIf for Grade Determination

  16. Two types: horizontal and vertical Properties: Name, Value, Max, Min, LargeChange, SmallChange Events: Change It’s like a thermostat you can set by moving the mouse over the button Scroll Bars Control

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

  18. Example of Select Case to Determine LetterGrade Select Case AverageScore Case Is >= 90 Grade = “A” Case Is >= 80 Grade = “B” Case Is >= 70 Grade = “C” Case Is >= 60 Grade = “D” Case Else Grade = “F” End Select

  19. We’ll do multiple decisions both ways in VB There are virtually no limitations on what you can do with the “if elseif” construct The are limitations to what you can do with the Select Case statement when you can live with the limitations, I find the Select Case more straightforward and easier to use you have to learn the limitations and how to meet the requirements of the Case statement IF-Then-ElseIf Vs. Select Case

  20. Conditions for Case statement can be in 3 forms: Value or expression Case 91, 92, 93 Range of values Case 90 to 100 Comparison condition Case Is > 89 Not as powerful as IF statement E.g. Case does not apply to compound conditions while IF does Case Conditions

  21. Decisions within decisions are know as nested decisions Interior decision must be an alternative of outer decision More Complex Decisions- Nested Decisions

  22. Need to check if employee is hourly before checking for overtime: If PayType = “Hourly” then If HoursWorked > 40 Then gross pay = 40 * PayRate + 1.5 * _ (HoursWorked-40) * PayRate Else gross pay = HoursWorked * PayRate End If Else gross pay = 40 * PayRate End If Example of Nested Decisions

  23. 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 (clicked on) item. We can test the Text property to determine which item was selected. Note that the text property is not available in the properties list because it can’t happen until run time Using the List Box

  24. In order to place items in the list, you have to overcome a problem. If you hit the enter key between items, you leave the list property in the properties window The solution is to do Ctrl-Enter between items Just as I have recommended all along that you clear the Text property of a text box at design time, I encourage you to: Either place useful entries in the list property of your list box at design time, or At least clear it out. Enter items in the List Property

  25. It is possible to add items to the list property of a list box at run time, but we’ll get to that later For now, we will only describe the Click event for a list box. Heck, that’s all we’ve done for other controls Review of what we know about list box: Properties: Name, Text, List Events: Click There are more Enter items to the List property(Cont.)

  26. In the previous Payroll (nested) example: Dim strPayType as string strPayType = lstPayType.Text If strPayType = "" Then MsgBox "Select a pay type first" Exit Sub End If Example of Using List Box

  27. Decisions that combine two or more test conditions using logical operators are known as compound decisions More Complex Decisions- Compound Decisions

  28. Logical operators may also be used when more complex conditions are required. The three main logical operators in VB are: And Or Not Logical Operators

  29. And. The logical operator And is used to connect two or more conditions. When two conditions are connected using And, both of the individual conditions must be true for the entire condition to be true. The general syntax of the And operator is: cond1 And cond2 The AND Operator

  30. Or. The logical operator Or is also used to connect two or more conditions. When two conditions are connected using Or, Either one, the other or both conditions need to be true for the entire condition to be true. This Or is sometimes referred to as an inclusive-Or. The general syntax of the Or operator is: cond1 Or cond2 Special case: Xor There is only one condition be true The OR Operator

  31. Not. The logical operator Not is used to negate a condition. The use of Not in front of a cond1 means that the complex condition is True only when the simple condition is false and vice versa. The general syntax of the Not operator is: NOT cond1 The NOT Operator

  32. Assume that we have the variables X = 5; Y = 8. Are the following compound conditions True or False? X < Y And Y <> 8 X <> 5 Or Y >= 0 Not Y = 8 Example

  33. Using compound condition to test for average AND number of absences If AverageScore >= 90 AND Absences < 3 then Grade = “A” Else If AverageScore >= 80 AND Absences < 5 then Grade = “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(Grade problem revisit)

  34. The Form_Load event occurs when the project is started and the form is loaded. Example in Grade Problem Code from a command button can be cut (or copied) and then pasted into the form_load event Using the Form_Load Event

  35. Everyone makes mistakes Let the program tell you what is wrong Debugging

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

  37. Breakpoints The program runs until it reaches a breakpoint; then it breaks (synonymous for pauses) You can look at things in several ways Flyover of a variable The immediate window [others, such as the locals and watch windows, will be covered later] Tools

  38. 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 You can replace Print with “ ? ” This can enable you to find errors in the code Debugging With the Immediate Window

More Related