decisions (conditional programming) chapter 5 (sec. 5.1 & 5.2)
Post on 20-Dec-2015
231 views
TRANSCRIPT
![Page 1: Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)](https://reader036.vdocuments.net/reader036/viewer/2022062320/56649d405503460f94a1aeae/html5/thumbnails/1.jpg)
Decisions
(Conditional Programming)
Chapter 5(Sec. 5.1 & 5.2)
![Page 2: Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)](https://reader036.vdocuments.net/reader036/viewer/2022062320/56649d405503460f94a1aeae/html5/thumbnails/2.jpg)
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)](https://reader036.vdocuments.net/reader036/viewer/2022062320/56649d405503460f94a1aeae/html5/thumbnails/3.jpg)
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)](https://reader036.vdocuments.net/reader036/viewer/2022062320/56649d405503460f94a1aeae/html5/thumbnails/4.jpg)
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)](https://reader036.vdocuments.net/reader036/viewer/2022062320/56649d405503460f94a1aeae/html5/thumbnails/5.jpg)
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)](https://reader036.vdocuments.net/reader036/viewer/2022062320/56649d405503460f94a1aeae/html5/thumbnails/6.jpg)
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)](https://reader036.vdocuments.net/reader036/viewer/2022062320/56649d405503460f94a1aeae/html5/thumbnails/7.jpg)
Conditional statements
• Null else
• If-then-else
• Nested if-then-else
![Page 8: Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)](https://reader036.vdocuments.net/reader036/viewer/2022062320/56649d405503460f94a1aeae/html5/thumbnails/8.jpg)
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)](https://reader036.vdocuments.net/reader036/viewer/2022062320/56649d405503460f94a1aeae/html5/thumbnails/9.jpg)
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)](https://reader036.vdocuments.net/reader036/viewer/2022062320/56649d405503460f94a1aeae/html5/thumbnails/10.jpg)
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)](https://reader036.vdocuments.net/reader036/viewer/2022062320/56649d405503460f94a1aeae/html5/thumbnails/11.jpg)
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)](https://reader036.vdocuments.net/reader036/viewer/2022062320/56649d405503460f94a1aeae/html5/thumbnails/12.jpg)
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)](https://reader036.vdocuments.net/reader036/viewer/2022062320/56649d405503460f94a1aeae/html5/thumbnails/13.jpg)
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)](https://reader036.vdocuments.net/reader036/viewer/2022062320/56649d405503460f94a1aeae/html5/thumbnails/14.jpg)
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)](https://reader036.vdocuments.net/reader036/viewer/2022062320/56649d405503460f94a1aeae/html5/thumbnails/15.jpg)
Flowcharts to the rescue!
![Page 16: Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)](https://reader036.vdocuments.net/reader036/viewer/2022062320/56649d405503460f94a1aeae/html5/thumbnails/16.jpg)
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)](https://reader036.vdocuments.net/reader036/viewer/2022062320/56649d405503460f94a1aeae/html5/thumbnails/17.jpg)
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)](https://reader036.vdocuments.net/reader036/viewer/2022062320/56649d405503460f94a1aeae/html5/thumbnails/18.jpg)
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)](https://reader036.vdocuments.net/reader036/viewer/2022062320/56649d405503460f94a1aeae/html5/thumbnails/19.jpg)
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)](https://reader036.vdocuments.net/reader036/viewer/2022062320/56649d405503460f94a1aeae/html5/thumbnails/20.jpg)
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)](https://reader036.vdocuments.net/reader036/viewer/2022062320/56649d405503460f94a1aeae/html5/thumbnails/21.jpg)
Homework