Download presentation
Presentation is loading. Please wait.
1
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
2
Most programs make many decisions This is called programming logic Condition Action
3
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.
4
Relational operators = < <= > >= equal not equal less than less than or equal to greater than greater than or equal to
5
Examples - relational operators 3 > 1 = true 3 < 1 = false 3 >= 1 = true 3 3 = false “a” < “b” = true “abc” < “abd” = true
6
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 ( )!!!
7
Conditional statements Null else If-then-else Nested if-then-else
8
Null else if condition then action end if action Is it true? yn
9
if-then-else if condition then action1 else action2 end if action2 action1 Is it true? yn
10
Nested if’s If condition1 Then action1 Else If condition2 Then action2 Else action3 End If a1 a3a2
11
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)
12
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
13
Sometimes programming logic is fairly straightforward Page 218, #28 –Design the interface –Draw the flowchart Page 218, #29 –Design the interface –Draw the flowchart
14
And sometimes it can get rather complicated... Page 219, #38 –Design the interface –Draw the flowchart
15
Flowcharts to the rescue!
16
Logical operators help too... If condition1 Then If condition2 Then action End If If cond1 and cond2 Then action EndIf
17
When does “or” come in handy? If cond1 Then action EndIf If cond2 Then action EndIf If cond1 or cond2 Then action EndIf Draw the flowchart...
18
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
19
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 & above B - 80-89 C - 70-79 D - 60-69 F - 0-59
20
Format of Select Case block Select Case selector Case valuelist1 action1 Case valuelist2 action2. Case Else alternate action End Select optional valuelists may contain: constants variables expressions inequality preceded by IS range of values selector may be: a variable an expression
21
Homework
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.