Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A...
-
Upload
bertram-robertson -
Category
Documents
-
view
237 -
download
12
Transcript of Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A...
![Page 1: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/1.jpg)
Chapter 4:Making Decisions
![Page 2: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/2.jpg)
Understanding Logic-Planning Tools and Decision Making
• Pseudocode– A tool that helps programmers plan a program’s logic by
writing plain English statements• Flowchart– You write the steps in diagram form as a series of shapes
connected by arrows
2Microsoft Visual C# 2012, Fifth Edition
![Page 3: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/3.jpg)
Making Decisions Using the if Statement
• if statement– Used to make a single-alternative decision
• Block– One or more statements contained within a pair of curly
braces
3Microsoft Visual C# 2012, Fifth Edition
![Page 4: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/4.jpg)
Making Decisions Using the if Statement (cont’d.)
4Microsoft Visual C# 2012, Fifth Edition
![Page 5: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/5.jpg)
Making Decisions Using the if Statement (cont’d.)
5Microsoft Visual C# 2012, Fifth Edition
![Page 6: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/6.jpg)
Making Decisions Using the if Statement (cont’d.)
6Microsoft Visual C# 2012, Fifth Edition
![Page 7: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/7.jpg)
Making Decisions Using the if Statement (cont’d.)
7Microsoft Visual C# 2012, Fifth Edition
![Page 8: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/8.jpg)
Making Decisions Using the if Statement (cont’d.)
• Nested if– One decision structure, or if statement, is contained
within another– Decision structures can be nested to multiple levels– If an outer level if statement fails or returns a false
result, all inner blocks of code are ignored– Creating too many levels can result in code that is difficult
to understand and maintain
8Microsoft Visual C# 2012, Fifth Edition
![Page 9: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/9.jpg)
Making Decisions Using the if Statement (cont’d.)
9Microsoft Visual C# 2012, Fifth Edition
![Page 10: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/10.jpg)
Making Decisions Using the if Statement (cont’d.)
10Microsoft Visual C# 2012, Fifth Edition
![Page 11: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/11.jpg)
Making Decisions Using the if-else Statement
• Dual-alternative decisions– Have two possible outcomes
• if-else statement– Used to perform one action when a Boolean expression
evaluates as true, and an alternate action when it evaluates as false
11Microsoft Visual C# 2012, Fifth Edition
![Page 12: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/12.jpg)
Making Decisions Using the if-else Statement (cont’d.)
12Microsoft Visual C# 2012, Fifth Edition
![Page 13: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/13.jpg)
Making Decisions Using the if-else Statement (cont’d.)
13Microsoft Visual C# 2012, Fifth Edition
![Page 14: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/14.jpg)
Using Compound Expressions in if Statements
• You can combine multiple decisions into a single if statement– Use a combination of AND and OR operators– This is an alternative to a nested if in some
circumstances
14Microsoft Visual C# 2012, Fifth Edition
![Page 15: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/15.jpg)
Using the Conditional AND Operator
• Conditional AND operator– Determines whether two expressions are both true– Written as two ampersands (&&)– You must include a complete Boolean expression on each
side of the operator
15Microsoft Visual C# 2012, Fifth Edition
![Page 16: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/16.jpg)
Using the Conditional AND Operator (cont’d)
16Microsoft Visual C# 2012, Fifth Edition
![Page 17: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/17.jpg)
17Microsoft Visual C# 2012, Fifth Edition
Using the Conditional AND Operator (cont’d)
![Page 18: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/18.jpg)
Using the Conditional OR Operator
• Conditional OR operator– Used when you want some action to occur even if only one
of two conditions is true– Written as two pipes ||– You must include a complete Boolean expression on each
side of the operator
18Microsoft Visual C# 2012, Fifth Edition
![Page 19: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/19.jpg)
Using the Conditional OR Operator (cont’d)
19Microsoft Visual C# 2012, Fifth Edition
![Page 20: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/20.jpg)
20Microsoft Visual C# 2012, Fifth Edition
Using the Conditional OR Operator (cont’d)
![Page 21: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/21.jpg)
Combining AND and OR Operators
21Microsoft Visual C# 2012, Fifth Edition
![Page 22: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/22.jpg)
Making Decisions Using the switch Statement
• switch structure– Tests a single variable against a series of exact matches
• Keywords– switch, case, break, and default
• A switch does not need a default case– Good programming practice to include one
22Microsoft Visual C# 2012, Fifth Edition
![Page 23: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/23.jpg)
Making Decisions Using the switch Statement (cont’d.)
• “No fall through rule”– Not allowing code to reach the end of a case– Use a break statement at the end of each case
23Microsoft Visual C# 2012, Fifth Edition
![Page 24: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/24.jpg)
Making Decisions Using the switch Statement (cont’d.)
24Microsoft Visual C# 2012, Fifth Edition
![Page 25: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/25.jpg)
Making Decisions Using the switch Statement (cont’d.)
25Microsoft Visual C# 2012, Fifth Edition
![Page 26: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/26.jpg)
Making Decisions Using the switch Statement (cont’d.)
• You can use multiple labels to govern a list of statements
26Microsoft Visual C# 2012, Fifth Edition
![Page 27: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/27.jpg)
Using the Conditional Operator
• Conditional operator– Used as an abbreviated version of the if-else
statement– A ternary operator that takes three parameters
• Syntax– testExpression ? trueResult : falseResult;
• Example– Console.WriteLine((testScore >= 60) ? "Pass" : " Fail");
27Microsoft Visual C# 2012, Fifth Edition
![Page 28: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/28.jpg)
Using the NOT Operator
• NOT operator– Written as an exclamation point (!)– Negates the result of any Boolean expression
• If the Boolean expression is true, ! makes it false• If the Boolean expression is false, ! makes it true
– Logic using the ! operator can be difficult to read and analyze
– The ! operator has a higher precedence than && and ||
28Microsoft Visual C# 2012, Fifth Edition
![Page 29: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/29.jpg)
Avoiding Common Errors When Making Decisions
• Most frequent errors include:– Using the assignment operator (=) instead of the
comparison operator (==)– Inserting a semicolon after the Boolean expression in an if statement
– Failing to block a set of statements with curly braces– Failing to include a complete Boolean expression on each
side of an && or || operator
29Microsoft Visual C# 2012, Fifth Edition
![Page 30: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/30.jpg)
Performing Accurate and Efficient Range Checks
• Range check– A series of if statements that determine whether a value
falls within a specified range
• Problem
30Microsoft Visual C# 2012, Fifth Edition
![Page 31: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/31.jpg)
Performing Accurate and Efficient Range Checks (cont’d.)
• Solutionif(saleAmount >= 1000) commissionRate = 0.08;else if(saleAmount >= 500) commissionRate = 0.06;else commissionRate = 0.05;
31Microsoft Visual C# 2012, Fifth Edition
![Page 32: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/32.jpg)
Using && and || Appropriately
• Problem– Print an error message when an employee’s hourly pay
rate is less than $5.65 and when an employee’s hourly pay rate is greater than $60
• Solutionif(payRate < 5.65 || payRate > 60) Console.WriteLine ("Error in pay rate");
32Microsoft Visual C# 2012, Fifth Edition
![Page 33: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/33.jpg)
Using the ! Operator Correctly
• Problem– Make sure that if the sales code is not ‘A’ or ‘B’, the
customer gets a 10% discount
• Solutionsif(salesCode != 'A' && salesCode != 'B') discount = 0.10;
if(!(salesCode == 'A' || salesCode == 'B')) discount = 0.10;
33Microsoft Visual C# 2012, Fifth Edition
![Page 34: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/34.jpg)
Decision-Making Issues in GUI Programs
• Making a decision within a method in a GUI application is no different from making one in a console application
• You can use if, if…else, and switch statements in the same ways
• GUI programs use controls to make decisions– The user clicks on a Button, which causes an event to fire– The user chooses one of several RadioButtons– The user chooses an item from a ListBox
34Microsoft Visual C# 2012, Fifth Edition
![Page 35: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/35.jpg)
Decision-Making Issues in GUI Programs (cont’d.)
35Microsoft Visual C# 2012, Fifth Edition
![Page 36: Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.](https://reader030.fdocuments.in/reader030/viewer/2022012910/56649eb75503460f94bc192e/html5/thumbnails/36.jpg)
Decision-Making Issues in GUI Programs (cont’d.)
36Microsoft Visual C# 2012, Fifth Edition