CS 106, Winter 2009 Class 7, Section 4

29
1 CS 106, Winter 2009 Class 7, Section 4 Slides by: Dr. Cynthia A. Brown, [email protected] Instructor section 4: Dr. Herbert G. Mayer, [email protected]

description

CS 106, Winter 2009 Class 7, Section 4. Slides by: Dr. Cynthia A. Brown, [email protected] Instructor section 4: Dr. Herbert G. Mayer, [email protected]. 1. Implementing a Process. Briefly describe the process - PowerPoint PPT Presentation

Transcript of CS 106, Winter 2009 Class 7, Section 4

Page 1: CS 106, Winter 2009 Class 7, Section 4

1

CS 106, Winter 2009Class 7, Section 4

Slides by: Dr. Cynthia A. Brown, [email protected] section 4: Dr. Herbert G. Mayer, [email protected]

Page 2: CS 106, Winter 2009 Class 7, Section 4

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.

Page 3: CS 106, Winter 2009 Class 7, Section 4

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.

Page 4: CS 106, Winter 2009 Class 7, Section 4

4

FlowchartPush Buy button

Input a number?yes no

Handle bad input

end

Compute costAdd tip

end

Print result

Page 5: CS 106, Winter 2009 Class 7, Section 4

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.

Page 6: CS 106, Winter 2009 Class 7, Section 4

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

Page 7: CS 106, Winter 2009 Class 7, Section 4

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

Page 8: CS 106, Winter 2009 Class 7, Section 4

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?

Page 9: CS 106, Winter 2009 Class 7, Section 4

9

Examples of True/False Expressions

Dim varA, varB, varC As DoublevarA = 1varB = 2varC = 2

varA < varB is truevarA <= varB is truevarC < varB is falsevarC <= varB is truevarB = varC is true ‘note dual role of the symbol =

Page 10: CS 106, Winter 2009 Class 7, Section 4

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

Page 11: CS 106, Winter 2009 Class 7, Section 4

11

And

• And is true if and only if both its arguments are true

• This is called a truth table

X Y X And Y

T T T

T F F

F T F

F F F

Page 12: CS 106, Winter 2009 Class 7, Section 4

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”

X Y X Or Y

T T T

T F T

F T T

F F F

Page 13: CS 106, Winter 2009 Class 7, Section 4

13

More Complex Expressions

• These can be worked out using truth tables• Consider (Not X) or (X and Y)

X Y Not X X and Y Not X or (X and Y)

T T F T T

T F F F F

F T T F T

F F T F T

Page 14: CS 106, Winter 2009 Class 7, Section 4

14

Another ExampleShow Not (A And B) is equal to (Not A) Or (Not B)

A B A And B Not (A and B)

T T T F

T F F T

F T F T

F F F T

A B Not A Not B (Not A) Or (Not B)

T T F F F

T F F T T

F T T F T

F F T T T

Page 15: CS 106, Winter 2009 Class 7, Section 4

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)

Page 16: CS 106, Winter 2009 Class 7, Section 4

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

action1Else

action2End If

Page 17: CS 106, Winter 2009 Class 7, Section 4

17

If Block Example‘ compute shipping charge, with a discount for more expensive orders

If Price < DiscountShippingPrice ThenShippingCharge = Price * ShippingChargePercentPrice = Price + ShippingCharge

ElseShippingCharge = Price * DiscountShippingChargePercentPrice = Price + Shipping Charge

End If

Page 18: CS 106, Winter 2009 Class 7, Section 4

18

If Block Example: Variation‘ compute shipping charge, with a discount for more expensive orders

If Price < DiscountShippingPrice ThenShippingCharge = Price * ShippingChargePercent

ElseShippingCharge = Price * DiscountShippingChargePercent

End If

Price = Price + Shipping Charge

Page 19: CS 106, Winter 2009 Class 7, Section 4

19

If Block Flowchart

Condition true?yes no

Perfom action 1 Perfom action 2

Bringing the branches back togethergives the flowchart a better structure

Page 20: CS 106, Winter 2009 Class 7, Section 4

20

If Block Options• The Else part can be omitted:If condition Then

action1End If

• There can be multiple ElseIf branchesIf condition1 Then ‘if condition1 is true do action1, end

action1ElseIf condition2 Then ‘if condition1 is F & condtion2 is T, do action2

action2Else ‘if both conditions are F, do action3

action3EndIf

Page 21: CS 106, Winter 2009 Class 7, Section 4

21

Single Branch If Block ‘ compute shipping charge if applicableIf Price < FreeShippingPrice Then

ShippingCharge = Price * ShippingChargePercentPrice = Price + ShippingCharge

End If‘ if the block is not executed the price is unchanged

Page 22: CS 106, Winter 2009 Class 7, Section 4

22

Multiple Branch If Block‘ Set thank you message based on tip sizeTipPercent = (TipAmount/BaseCharge) * 100If 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

Page 23: CS 106, Winter 2009 Class 7, Section 4

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

Page 24: CS 106, Winter 2009 Class 7, Section 4

24

Nested If’s‘ Select title based on language and sexIf Language = “French” Then

If Sex = Female ThenTitle = “Mademoiselle”ElseTitle = “Monsieur”Endif

ElseIf Language = “English” Then

If Sex = Female ThenTitle = “Miss”

Else Title = “Mister”EndIf

EndIf

Page 25: CS 106, Winter 2009 Class 7, Section 4

25

Converting to ElseIfs‘ Select title based on language and sexIf Language = “French” And Sex = Female Then

Title = “Mademoiselle”ElseIf Language = “French” And Sex = Male Then

Title = “Monsieur”ElseIf Language = “English” And Sex = Female Then

Title = “Miss”ElseIf Language = “English” And Sex = Male Then

Title = “Mister”Else

Title = “” ‘ it’s always best to have an else caseEndIf

Page 26: CS 106, Winter 2009 Class 7, Section 4

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

Page 27: CS 106, Winter 2009 Class 7, Section 4

27

Case Block exampleAssume position is a variable with a value between 1 and some number from 2 to 20

Select Case postionCase 1txtOutcome.Text = “Win” ‘several lines of code could go hereCase 2txtOutcome.Text = “Place”Case 3txtOutcome.Text = “Show”Case 4,5txtOutcome.Text = “Close but no cigar”Case ElsetxtOutcome.Text = “Out of the money”End Select

Page 28: CS 106, Winter 2009 Class 7, Section 4

28

BREAK

10 minutes

Page 29: CS 106, Winter 2009 Class 7, Section 4

29

Review of Assignment Program