1 / 29

CS 106, Winter 2009 Class 7, Section 4

CS 106, Winter 2009 Class 7, Section 4. Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer, herb@cs.pdx.edu. 1. Implementing a Process. Briefly describe the process

amable
Download Presentation

CS 106, Winter 2009 Class 7, Section 4

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. CS 106, Winter 2009Class 7, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer, herb@cs.pdx.edu 1

  2. Implementing a Process • Briefly describe the process • Elaborate it using use cases till you understand how it should work in all situations. • Figure out what the objects are and what their events are. Decide what internal state the process needs: what it needs to remember. • Develop tests based on the use cases. • Design the user interface. • Develop a flowchart for each event, and walk through the use cases and tests to validate them. • Implement the process. In VB, use VB controls for objects, VB subroutines for events, and global variables (or fields of controls) for remembering values. • Test your implementation.

  3. Implementation in VB • Start by building the user interface. Include all the objects you need. Give them meaningful names. • Set up a subroutine for each event. • Use Dim statements to define your global variables. Use meaningful names! • Write some code and test it. Test as you go along. • Document everything with comments. • Save early and often. Be sure to save your project to your flash drive at the end of each programming session.

  4. Flowchart Push Buy button yes no Input a number? Compute cost Add tip Handle bad input end Print result end

  5. Enlarging our Repertoire • So far we know how to write VB statements that change the values of properties of controls. These can be used to write information to text boxes, write labels, change the color of controls, etc. • Our next type of statement will be a conditional: it allows us to change what the program does based on the outcome of a test.

  6. Tests and Choices • To allow a program to make choices based on conditions we need to introduce a new programming construct • But first we look at how we program the tests that will determine which branch our program takes

  7. Boolean Expressions • A Boolean expression, like a Boolean variable, is one that can evaluate to true or false • The constants True and False are the simplest Boolean expressions • More interesting examples are created by, for example, comparing two things • Complex Boolean expressions are built up out of simple ones using Boolean operations

  8. Relational Operators • Relational Operators are used to create simple Boolean expressions • They include: = equal? < > not equal? < less than? <= less than or equal? > greater than? >= greater than or equal?

  9. Examples of True/False Expressions Dim varA, varB, varCAs Double varA = 1 varB = 2 varC = 2 varA < varB is true varA <= varB is true varC < varB is false varC <= varB is true varB = varC is true ‘note dual role of the symbol =

  10. Logical Operators • More complex Boolean expressions are built using Boolean operators. • The most common Boolean operators are And, Or, and Not • Other Boolean operators, such as exclusive or, are available if needed • Not reverses the truth of its argument: Not (True) = False and vice-versa

  11. And • And is true if and only if both its arguments are true • This is called a truth table

  12. Or • Or is true if and only if at least one of its arguments is true. This differs somewhat from the usual meaning of “or”

  13. More Complex Expressions • These can be worked out using truth tables • Consider (Not X) or (X and Y)

  14. Another Example Show Not (A And B) is equal to (Not A) Or (Not B)

  15. A Couple of Pointers • Some math/relational expressions do not translate directly. For example, A < B <= C would be written as (A < B) And (B <= C) • Be careful with And and Or. Normally both parts of an And expression are evaluated even if the first one is false, and both parts of an Or expression are evaluated even if the first one is true. Standard example: (X = 0) Or (Y/X <5) (could cause program to blow up if X = 0)

  16. If Block • VB uses Boolean expressions, along with If Blocks, to control the flow of execution of a program If condition Then ‘here condition is a Boolean expression action1 Else action2 End If

  17. If Block Example ‘ compute shipping charge, with a discount for more expensive orders If Price < DiscountShippingPrice Then ShippingCharge = Price * ShippingChargePercent Price = Price + ShippingCharge Else ShippingCharge = Price * DiscountShippingChargePercent Price = Price + Shipping Charge End If

  18. If Block Example: Variation ‘ compute shipping charge, with a discount for more expensive orders If Price < DiscountShippingPrice Then ShippingCharge = Price * ShippingChargePercent Else ShippingCharge = Price * DiscountShippingChargePercent End If Price = Price + Shipping Charge

  19. If Block Flowchart yes no Condition true? Perfom action 1 Perfom action 2 Bringing the branches back together gives the flowchart a better structure

  20. If Block Options • The Else part can be omitted: If condition Then action1 End If • There can be multiple ElseIf branches If condition1 Then ‘if condition1 is true do action1, end action1 ElseIf condition2 Then ‘if condition1 is F & condtion2 is T, do action2 action2 Else ‘if both conditions are F, do action3 action3 EndIf

  21. Single Branch If Block ‘ compute shipping charge if applicable If Price < FreeShippingPrice Then ShippingCharge = Price * ShippingChargePercent Price = Price + ShippingCharge End If ‘ if the block is not executed the price is unchanged

  22. Multiple Branch If Block ‘ Set thank you message based on tip size TipPercent = (TipAmount/BaseCharge) * 100 If TipPercent < 15 Then txtThankYou.Text = “Thanks.” ElseIf TipPercent < 20 Then txtThankYou.Text = “Thank you and have a nice day.” ElseIf TipPercent < 25 Then txtThankYou.Text = “Thank you very much! Have a nice day.” Else txtThankYou.Text = “Thank you!! Have a GREAT Day!” End If

  23. More Options • You can nest entire if blocks inside the if or else part of another if block • Nesting more than one or two deep is strongly discouraged! It makes the program hard to read • Try to use Elseif or more complex conditions instead

  24. Nested If’s ‘ Select title based on language and sex If Language = “French” Then If Sex = Female Then Title = “Mademoiselle” Else Title = “Monsieur” Endif Else If Language = “English” Then If Sex = Female Then Title = “Miss” Else Title = “Mister” EndIf EndIf

  25. Converting to ElseIfs ‘ Select title based on language and sex If Language = “French” AndSex = Female Then Title = “Mademoiselle” ElseIfLanguage = “French” AndSex = Male Then Title = “Monsieur” ElseIfLanguage = “English” AndSex = Female Then Title = “Miss” ElseIfLanguage = “English” AndSex = Male Then Title = “Mister” Else Title = “” ‘ it’s always best to have an else case EndIf

  26. Select Case Blocks • The select case block can be used when there are multiple options to choose from • It can simplify program structure • It makes the logical structure of the program clear when a nested if or long Elseif structure might not

  27. Case Block example Assume position is a variable with a value between 1 and some number from 2 to 20 SelectCasepostion Case 1 txtOutcome.Text = “Win” ‘several lines of code could go here Case 2 txtOutcome.Text = “Place” Case 3 txtOutcome.Text = “Show” Case 4,5 txtOutcome.Text = “Close but no cigar” Case Else txtOutcome.Text = “Out of the money” End Select

  28. BREAK 10 minutes

  29. Review of Assignment Program

More Related