Chapter 1 The Engine That Runs The Economy. Chapter 1.1: Decisions, Decisions.
Chapter 5 – Decisions
description
Transcript of Chapter 5 – Decisions
![Page 1: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/1.jpg)
Chapter 5 - VB 2005 by Schneider
1
Chapter 5 – Decisions
• 5.1 Relational and Logical Operators
• 5.2 If Blocks
![Page 2: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/2.jpg)
Chapter 5 - VB 2005 by Schneider
2
5.1 Relational and Logical Operators
• Relational Operators
• Logical Operators
• Boolean Data Type
![Page 3: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/3.jpg)
Chapter 5 - VB 2005 by Schneider
3
Relational Operators
< less than
<= less than or equal to
> greater than
>= greater than or equal to
= equal to
<> not equal to
ANSI values are used to decide order for strings.
![Page 4: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/4.jpg)
Chapter 5 - VB 2005 by Schneider
4
Boolean Data Type
• An expression or variable that evaluates to either True or False is said to have Boolean data type.
• Example:
The statement
txtBox.Text = (2+3)<6
displays True in the text box.
![Page 5: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/5.jpg)
Chapter 5 - VB 2005 by Schneider
5
Example
When a = 3, b = 4
(a + b) < 2 * a
3 + 4 = 7 2 * 3 = 6
7 is NOT less than 6 and the value of the expression is False
![Page 6: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/6.jpg)
Chapter 5 - VB 2005 by Schneider
6
Relational Operator Notes
• Relational operators are binary – they require an operand on both sides of the operator
• Value of a relational expression will always be True or False
• Expressions are evaluated from left to right with no order of operations
![Page 7: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/7.jpg)
Chapter 5 - VB 2005 by Schneider
7
Logical Operators
• Used with Boolean expressions
• Not – makes a False expression True and vice versa
• And – will yield a True if and only if both expressions are True
• Or – will yield a True if at least one of both expressions are True
![Page 8: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/8.jpg)
Chapter 5 - VB 2005 by Schneider
8
Example
To test if n falls between 2 and 5:
(2 < n ) And ( n < 5 )
A complete relational expression must be
on either side of the logical operators And
and Or.
![Page 9: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/9.jpg)
Chapter 5 - VB 2005 by Schneider
9
Syntax error
The following is NOT a valid way to test if n falls between 2 and 5:
(2 < n < 5 )
![Page 10: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/10.jpg)
Chapter 5 - VB 2005 by Schneider
10
Order of Operations
The order of operations for evaluating Boolean expressions is:
1. Arithmetic operators
2. Relational operators
3. Logical operators
![Page 11: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/11.jpg)
Chapter 5 - VB 2005 by Schneider
11
Arithmetic Order of Operations
1.Parenthesis
2.Exponentiation
3.Division and multiplication
4.Addition and subtraction
![Page 12: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/12.jpg)
Chapter 5 - VB 2005 by Schneider
12
Relational Order of Operations
They all have the same precedence
![Page 13: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/13.jpg)
Chapter 5 - VB 2005 by Schneider
13
Logical Order of Operations
1. Not
2. And
3. Or
![Page 14: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/14.jpg)
Chapter 5 - VB 2005 by Schneider
14
Condition
• A condition is an expression involving relational and/or logical operators
• Result of the condition is Boolean – that is, True or False
![Page 15: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/15.jpg)
Chapter 5 - VB 2005 by Schneider
15
5.2 If Blocks
• If Block
• ElseIf Clause
![Page 16: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/16.jpg)
Chapter 5 - VB 2005 by Schneider
16
If BlockThe program will take a course of actionbased on whether a condition is true.
If condition Then action1Else action2End If
Will be executed if condition is true
Will be executed if condition is false
![Page 17: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/17.jpg)
Chapter 5 - VB 2005 by Schneider
17
Another example If block
If condition Then
action1
End If
Statement2
Statement3
Regardless of whether
the condition in the
If statement is True or
False, these statements
will be executed
![Page 18: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/18.jpg)
Chapter 5 - VB 2005 by Schneider
18
Pseudocode and Flowchart for an If Block
![Page 19: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/19.jpg)
Chapter 5 - VB 2005 by Schneider
19
Example 1: Form
txtFirstNum
txtSecondNum
txtResult
![Page 20: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/20.jpg)
Chapter 5 - VB 2005 by Schneider
20
Example 1: CodePrivate Sub btnFindLarger_Click(...) _ Handles btnFindLarger.Click Dim num1, num2, largerNum As Double num1 = CDbl(txtFirstNum.Text) num2 = CDbl(txtSecondNum.Text) If num1 > num2 Then largerNum = num1 Else largerNum = num2 End If txtResult.Text = "The larger number is " & largerNumEnd Sub
![Page 21: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/21.jpg)
Chapter 5 - VB 2005 by Schneider
21
Example 1: Output
![Page 22: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/22.jpg)
Chapter 5 - VB 2005 by Schneider
22
Example 2: Form
![Page 23: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/23.jpg)
Chapter 5 - VB 2005 by Schneider
23
Example 2: Partial CodeIf costs = revenue Then txtResult.Text = "Break even"Else If costs < revenue Then profit = revenue - costs txtResult.Text = "Profit is " & _ FormatCurrency(profit) Else loss = costs - revenue txtResult.Text = "Loss is " & _ FormatCurrency(loss) End IfEnd If
![Page 24: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/24.jpg)
Chapter 5 - VB 2005 by Schneider
24
Example 2: Output
![Page 25: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/25.jpg)
Chapter 5 - VB 2005 by Schneider
25
Example 3: Form
txtAnswer
txtSolution
![Page 26: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/26.jpg)
Chapter 5 - VB 2005 by Schneider
26
Example 3: CodePrivate Sub btnEvaluate_Click(...) _ Handles btnEvaluate.Click Dim answer As Double answer = CDbl(txtAnswer.Text) If (answer >= 0.5) And (answer <= 1) Then txtSolution.Text = "Good, " Else txtSolution.Text = "No, " End If txtSolution.Text &= "it holds about 3/4 of" _ & " a gallon."End Sub
![Page 27: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/27.jpg)
Chapter 5 - VB 2005 by Schneider
27
Example 3: Output
![Page 28: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/28.jpg)
Chapter 5 - VB 2005 by Schneider
28
ElseIf clauseIf condition1 Then action1ElseIf condition2 Then action2ElseIf condition3 Then action3Else action4End If
![Page 29: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/29.jpg)
Chapter 5 - VB 2005 by Schneider
29
Example 5: Form
txtFirstNum
txtSecondNum
txtResult
![Page 30: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/30.jpg)
Chapter 5 - VB 2005 by Schneider
30
Example 5: CodePrivate Sub btnFindLarger_Click(...) _ Handles btnFindLarger.Click Dim num1, num2 As Double num1 = CDbl(txtFirstNum.Text) num2 = CDbl(txtSecondNum.Text) If (num1 > num2) Then txtResult.Text = "Larger number is " & num1 ElseIf (num2 > num1) Then txtResult.Text = "Larger number is " & num2 Else txtResult.Text = "The two are equal." End IfEnd Sub
![Page 31: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/31.jpg)
Chapter 5 - VB 2005 by Schneider
31
Comments
• When one If block is contained inside another If block, the structure is referred to as nested If blocks.
• Care should be taken to make If blocks easy to understand.
![Page 32: Chapter 5 – Decisions](https://reader036.fdocuments.net/reader036/viewer/2022062309/56814f3d550346895dbce5ac/html5/thumbnails/32.jpg)
Chapter 5 - VB 2005 by Schneider
32
Simplified Nested If StatementIf cond1 Then If cond1 And cond2 Then
If cond2 Then action
action End If
End If
End If
NestedIf
LessConfusing