Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)

Post on 20-Dec-2015

223 views 0 download

transcript

Decisions

(Conditional Programming)

Chapter 5(Sec. 5.1 & 5.2)

Most programs make many decisions

• This is called programming logic

Condition

Action Action

Conditional statements• A condition is an expression that

evaluates to true or false

• It involves two types of operators

– relational

– logical (Boolean)

• A conditional statement is a statement in a program that contains a condition.

Relational operators=

< >

<

<=

>

>=

equal

not equal

less than

less than or equal to

greater than

greater than or equal to

Examples - relational operators

3 > 1 = true3 < 1 = false3 >= 1 = true3 < > 3 = false“a” < “b” = true“abc” < “abd” = true

Logical operators• and

p and q = true if both p & q are true• or

p or q = true if either p or q is true• not

not p = true if p is false• Use ( )!!!

Conditional statements

• Null else

• If-then-else

• Nested if-then-else

Null else

if condition then

action

end if action

Is it true? yn

if-then-else

if condition then

action1

else

action2

end if

action2 action1

Is it true? yn

Nested if’sIf condition1 Then

action1Else

If condition2 Then action2Else action3End If

End If

a1

a3 a2

Examples

• Null else:– p. 208, #4

• If - then - else:– p. 205-206, Ex. 1

• Nested:– p. 206, Ex. 2– p. 207, Ex. 3 (includes logical operator)

Good Style

• Use an End If for every If– your code should flow from a clearly designed

flowchart– do not follow the book’s approach to

Examples 5 or 6

ElseIf• See Figure 5.2, p. 211

Sometimes programming logic is fairly straightforward

• Page 218, #28– Design the interface – Draw the flowchart

• Page 218, #29– Design the interface – Draw the flowchart

And sometimes it can get rather complicated...

• Page 219, #38– Design the interface – Draw the flowchart

Flowcharts to the rescue!

Logical operators help too...If condition1 Then

If condition2 Then actionEnd If

End If

If cond1 and cond2 Thenaction

EndIf

When does “or” come in handy?

If cond1 Then

action

EndIf

If cond2 Then

action

EndIf

If cond1 or cond2 Then actionEndIf

Draw the flowchart...

Some problems from the book• Page 211 (Practice Problems 5.2) #2

• Page 217, #21 - #26

• Lab Today:

• In each of the following programs you MUST have one input procedure and one output procedure. The output procedure will call a FUNCTION to do the calculation

• Page 218-219, #29, #38

End Section 5.2

Select Case Blocks - Sec. 5.3

• Useful to replace deeply nested if’s– Example: Write a program that produces letter

grades for test scores using the traditional grading scale:

A - 90 & aboveB - 80-89C - 70-79D - 60-69F - 0-59

Format of Select Case block

Select Case selectorCase valuelist1

action1Case valuelist2

action2..

Case Elsealternate action

End Select

optional

valuelists may contain:•constants•variables•expressions•inequality preceded by IS•range of values

selector may be:•a variable•an expression

Homework