decisions (conditional programming) chapter 5 (sec. 5.1 & 5.2)

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

Post on 20-Dec-2015

226 views

Category:

Documents


0 download

TRANSCRIPT

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

Decisions

(Conditional Programming)

Chapter 5(Sec. 5.1 & 5.2)

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

Most programs make many decisions

• This is called programming logic

Condition

Action Action

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

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.

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

Relational operators=

< >

<

<=

>

>=

equal

not equal

less than

less than or equal to

greater than

greater than or equal to

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

Examples - relational operators

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

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

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 ( )!!!

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

Conditional statements

• Null else

• If-then-else

• Nested if-then-else

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

Null else

if condition then

action

end if action

Is it true? yn

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

if-then-else

if condition then

action1

else

action2

end if

action2 action1

Is it true? yn

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

Nested if’sIf condition1 Then

action1Else

If condition2 Then action2Else action3End If

End If

a1

a3 a2

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

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)

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

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

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

Sometimes programming logic is fairly straightforward

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

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

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

And sometimes it can get rather complicated...

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

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

Flowcharts to the rescue!

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

Logical operators help too...If condition1 Then

If condition2 Then actionEnd If

End If

If cond1 and cond2 Thenaction

EndIf

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

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

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

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

Page 19: Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 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

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

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

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

Homework