290 likes | 388 Views
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
E N D
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
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.
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.
Flowchart Push Buy button yes no Input a number? Compute cost Add tip Handle bad input end Print result end
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.
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
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
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?
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 =
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
And • And is true if and only if both its arguments are true • This is called a truth table
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”
More Complex Expressions • These can be worked out using truth tables • Consider (Not X) or (X and Y)
Another Example Show Not (A And B) is equal to (Not A) Or (Not B)
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)
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
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
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
If Block Flowchart yes no Condition true? Perfom action 1 Perfom action 2 Bringing the branches back together gives the flowchart a better structure
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
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
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
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
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
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
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
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
BREAK 10 minutes