An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.

Slides:



Advertisements
Similar presentations
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Advertisements

Selection (decision) control structure Learning objective
Objectives AND logic OR logic Evaluating compound conditions with multiple logical operators Precedence when combining AND and OR operators Efficiency.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
C++ for Engineers and Scientists Third Edition
CIS162AD - C# Decision Statements 04_decisions.ppt.
Programming Logic and Design Fourth Edition, Introductory
Programming Logic and Design Sixth Edition
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Computer Science Selection Structures.
Chapter 3 Making Decisions
CHAPTER 4: CONDITIONAL STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
Lecture Set 5 Control Structures Part A - Decisions Structures.
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Selection Structure If... Then.. Else Case. Selection Structure Use to make a decision or comparison and then, based on the result of that decision or.
Selection Control Structures Simple Program Design Third Edition A Step-by-Step Approach 4.
Programming Logic and Design, Second Edition, Comprehensive
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
CHAPTER 4: Selection Control Structure. Objectives  Use the relational comparison operators  Learn about AND logic  Learn about OR logic  Make selections.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Chapter 5: Making Decisions
WRITING CONTROL STRUCTURES (CONDITIONAL CONTROL).
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
Controlling Program Flow with Decision Structures.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 4 Making Decisions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Programming Logic and Design Fourth Edition, Introductory Chapter 2 Understanding Structure.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Java Programming Fifth Edition
Chapter 4: Decisions and Conditions
More on the Selection Structure
Selections Java.
Chapter 5 Decisions. Chapter 5 Decisions ssential uestion: How are Boolean expressions or operators used in everyday life?
Topics The if Statement The if-else Statement Comparing Strings
Topics The if Statement The if-else Statement Comparing Strings
Relational Operators Operator Meaning < Less than > Greater than
Three Special Structures – Case, Do While, and Do Until
Chapter 4: Control Structures I (Selection)
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
CHAPTER 4: Conditional Structures
Relational Operators.
Chapter 5 Decisions.
Chapter 3: Selection Structures: Making Decisions
The Selection Structure
Using C++ Arithmetic Operators and Control Structures
Controlling Program Flow
Lecture 9: Implementing Complex Logic
Presentation transcript:

An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions

An Object-Oriented Approach to Programming Logic and Design 2 Objectives Evaluate Boolean expressions to make comparisons Use the relational comparison operators Make decisions with objects Understand AND logic Understand OR logic

An Object-Oriented Approach to Programming Logic and Design 3 Objectives (continued) Use selections within ranges Understand common errors using range checks Understand precedence when combining AND and OR selections Understand the case structure Use a decision table

An Object-Oriented Approach to Programming Logic and Design 4 Evaluating Boolean Expressions to Make Comparisons People think computers are smart because computers can make decisions Dual-alternative, or binary, selection structure: logic flows to one of two alternatives; also called the if-then-else structure If no else clause, it is called the single- alternative, or unary selection structure; also called the if-then structure

An Object-Oriented Approach to Programming Logic and Design 5 Evaluating Boolean Expressions to Make Comparisons (continued) Example: if the answer to the question is yes, then do something else do somethingElse endif

An Object-Oriented Approach to Programming Logic and Design 6 Evaluating Boolean Expressions to Make Comparisons (continued)

An Object-Oriented Approach to Programming Logic and Design 7 Evaluating Boolean Expressions to Make Comparisons (continued) Example:

An Object-Oriented Approach to Programming Logic and Design 8 Evaluating Boolean Expressions to Make Comparisons (continued) Boolean expression – one that represents only one of two states, True or False

An Object-Oriented Approach to Programming Logic and Design 9 Using the Relational Comparison Operators Usually you compare values of the same type Three possible comparison decisions: –Both values are equal –First value is greater than second value –First value is less than second value Values to be compared can be variables or constants

An Object-Oriented Approach to Programming Logic and Design 10 Using the Relational Comparison Operators (continued) Each programming language has its own set of relational comparison operators, or comparison symbols Most languages allow the > and < signs for comparison Comparison operators require a value on each side of the operator

An Object-Oriented Approach to Programming Logic and Design 11 Using the Relational Comparison Operators (continued) Most languages support: –>= First value is greater than or equal to the second value –<= First value is less than or equal to the second value –The two values are not equal While not required, these comparisons simplify the code

An Object-Oriented Approach to Programming Logic and Design 12 Using the Relational Comparison Operators (continued) If a >= b is True, then a < b is False If a >= b is False, then a < b is True Rephrase the question and swap the action taken to make the same decision

An Object-Oriented Approach to Programming Logic and Design 13 Using the Relational Comparison Operators (continued) Example: if customerAge < 65 then discount = 0 else discount = 0.10 endif if customerAge >= 65 then discount = 0.10 else discount = 0 endif

An Object-Oriented Approach to Programming Logic and Design 14 Using the Relational Comparison Operators (continued) Negative comparisons can lead to double negatives – use caution!

An Object-Oriented Approach to Programming Logic and Design 15 Using the Relational Comparison Operators (continued ) Rephrase question to eliminate double negative:

An Object-Oriented Approach to Programming Logic and Design 16 Using the Relational Comparison Operators (continued)

An Object-Oriented Approach to Programming Logic and Design 17 Making Decisions with Objects Object-oriented programs make decisions about primitive types and objects Decision-making logic is the same for both

An Object-Oriented Approach to Programming Logic and Design 18 Making Decisions about Primitive Data Types Example with primitive data:

An Object-Oriented Approach to Programming Logic and Design 19 Making Decisions about Objects Decision making with objects may be more complex because the data is usually hidden Object instance names are references, or pointers, to the object Comparing pointers only determines if they point to the same or different memory locations; does NOT compare the value of the instance data You must compare the object’s data values

An Object-Oriented Approach to Programming Logic and Design 20 Making Decisions about Objects (continued)

An Object-Oriented Approach to Programming Logic and Design 21 Making Decisions about Objects (continued)

An Object-Oriented Approach to Programming Logic and Design 22 Understanding AND Logic You may need more than one selection structure to make a decision AND decision requires that both questions evaluate as True Also called a compound decision Requires a nested decision – a nested if

An Object-Oriented Approach to Programming Logic and Design 23 Understanding AND Logic (continued)

An Object-Oriented Approach to Programming Logic and Design 24 Understanding AND Logic (continued)

An Object-Oriented Approach to Programming Logic and Design 25 Understanding AND Logic (continued) Use care to match the else statements correctly!

An Object-Oriented Approach to Programming Logic and Design 26 Understanding AND Logic (continued)

An Object-Oriented Approach to Programming Logic and Design 27 Nesting and Decisions for Efficiency In an AND decision, either decision can come first (if a single action results) Efficiency may be improved by making a good choice for which decision comes first The choice may be based on the data: What is the outcome that is expected most often? Rule of thumb: first ask the question that is less likely to be true – may eliminate the need to evaluate the second question

An Object-Oriented Approach to Programming Logic and Design 28 Nesting and Decisions for Efficiency (continued) Example: Both produce the same decision

An Object-Oriented Approach to Programming Logic and Design 29 Combining Decisions in an AND Selection Most languages allow multiple comparisons using a logical AND operator Logical AND is equivalent to nested if statements Consider the order of the questions for efficiency

An Object-Oriented Approach to Programming Logic and Design 30 Combining Decisions in an AND Selection (continued)

An Object-Oriented Approach to Programming Logic and Design 31 Avoiding Common Errors in an AND Selection Error: Decisions are not nested!

An Object-Oriented Approach to Programming Logic and Design 32 Avoiding Common Errors in an AND Selection (continued) Using AND to handle a range of values requires two complete Boolean expressions Correct example: if itemsSold >= 5 AND itemsSold <= 10 then bonus = 75 endif if itemsSold >= 5 AND <= 10 then bonus = 75 endif Incorrect example:

An Object-Oriented Approach to Programming Logic and Design 33 Understanding OR Logic OR TrueOR decision requires that at least one of the questions evaluates to True

An Object-Oriented Approach to Programming Logic and Design 34 Writing Or Decisions for Efficiency ORIn an OR decision, first ask the question that is more likely to be True

An Object-Oriented Approach to Programming Logic and Design 35 Combining Decisions in an Or Selection ORLogical OR operator allows two or more questions to be asked in a single statement ORMost languages require a complete Boolean expression on each side of the OR operator

An Object-Oriented Approach to Programming Logic and Design 36 Combining Decisions in an Or Selection (continued)

An Object-Oriented Approach to Programming Logic and Design 37 Avoiding Common Errors in an Or Selection Unstructured Example:

An Object-Oriented Approach to Programming Logic and Design 38 Avoiding Common Errors in an Or Selection (continued) Warning! Casual use of English may use “and” and “or” incorrectly Be sure to state required decisions clearly and unambiguously when there are multiple decisions

An Object-Oriented Approach to Programming Logic and Design 39 Avoiding Common Errors in an Or Selection (continued)

An Object-Oriented Approach to Programming Logic and Design 40 Avoiding Common Errors in an Or Selection (continued) Most languages support a NOT operator Logical NOT operator reverses the meaning of a Boolean expression NOT operator is unary – place it in front of a single expression Example: if NOT (age < 21) then print “OK” endif

An Object-Oriented Approach to Programming Logic and Design 41 Using Selection Within Ranges A range check compares a variable to a series of values between limits Make comparisons using either the lowest or highest value in each range of values

An Object-Oriented Approach to Programming Logic and Design 42 Using Selection Within Ranges (continued)

An Object-Oriented Approach to Programming Logic and Design 43 Using Selection Within Ranges (continued)

An Object-Oriented Approach to Programming Logic and Design 44 Using Selection Within Ranges (continued)

An Object-Oriented Approach to Programming Logic and Design 45 Understanding Common Errors Using Range Checks Avoid logical paths that are unreachable Avoid asking questions that have just one possible answer or outcome

An Object-Oriented Approach to Programming Logic and Design 46 Understanding Common Errors Using Range Checks (continued)

An Object-Oriented Approach to Programming Logic and Design 47 Understanding Common Errors Using Range Checks (continued)

An Object-Oriented Approach to Programming Logic and Design 48 Understanding Common Errors Using Range Checks (continued)

An Object-Oriented Approach to Programming Logic and Design 49 Understanding Common Errors Using Range Checks (continued)

An Object-Oriented Approach to Programming Logic and Design 50 Understanding Precedence when Combining AND and Or Selections ORMost languages allow combination of multiple AND and OR operators in an expression ORWhen AND and OR are in the same statement, the And operator takes precedence ORUse parentheses to control which operators are evaluated first, or use nested if statements instead of ANDs and ORs

An Object-Oriented Approach to Programming Logic and Design 51 Understanding Precedence when Combining AND and Or Selections (continued) Examples: if age =65 AND rating=“G” then print “Discount applies” if (age =65) AND rating=“G” then print “Discount applies”

An Object-Oriented Approach to Programming Logic and Design 52 Understanding Precedence when Combining And and Or Selections (continued)

An Object-Oriented Approach to Programming Logic and Design 53 Understanding the Case Structure case structure allows a selection between multiple alternatives Improves the readability of the code, because it replaces “chained” if statements T ests a variable against a series of values, and executes only the code for that value

An Object-Oriented Approach to Programming Logic and Design 54 Understanding the Case Structure (continued)

An Object-Oriented Approach to Programming Logic and Design 55 Understanding the Case Structure (continued)

An Object-Oriented Approach to Programming Logic and Design 56 Using Decision Tables Decision table – a problem analysis tool consisting of: –Conditions –Possible combinations of Boolean values for the conditions –Possible actions based on the outcomes –A specific action corresponding to each Boolean value of each condition

An Object-Oriented Approach to Programming Logic and Design 57 Using Decision Tables (continued) Example: College dorm assignment rules: Students under age 21 who request a quiet dorm are assigned to Addams Hall Students under age 21 who do not request a quiet dorm are assigned to Grant Hall Students 21 and over who request a quiet dorm are assigned to Lincoln Hall Students 21 and over who do not request a quiet dorm are assigned to Lincoln Hall (the only dorm for over 21)

An Object-Oriented Approach to Programming Logic and Design 58 Using Decision Tables (continued) List all possible conditions that affect the outcome: –Age is under 21, or not –Requests quiet dorm, or not Determine all possible Boolean combinations for the conditions:

An Object-Oriented Approach to Programming Logic and Design 59 Using Decision Tables (continued) Add rows to list possible outcome actions:

An Object-Oriented Approach to Programming Logic and Design 60 Using Decision Tables (continued) Choose one outcome for each possible combination of conditions:

An Object-Oriented Approach to Programming Logic and Design 61 Using Decision Tables (continued) Write pseudocode to describe the first and second columns for which age is less than 21: if age <21 then if quietRequest = “Y” then assignedHall = “Addams” else assignedHall = “Grant” endif

An Object-Oriented Approach to Programming Logic and Design 62 Using Decision Tables (continued) Now add pseudocode to describe the third and fourth columns for which age is greater than or equal to 21: else if quietRequest = “Y” then assignedHall = “Lincoln” else assignedHall = “Lincoln” endif

An Object-Oriented Approach to Programming Logic and Design 63 Using Decision Tables (continued) But the state of “quietRequest” does not matter if the student is >= 21, so there is no need to test the “quietRequest” variable: else assignedHall = “Lincoln” endif

An Object-Oriented Approach to Programming Logic and Design 64 Using Decision Tables (continued)

An Object-Oriented Approach to Programming Logic and Design 65 Summary All decisions evaluate Boolean expressions Any two values can be compared using relational comparison operators AND operator allows two or more conditions to be tested in a single statement AND decision requires that both conditions are true in order to return a True result In an AND decision, first ask the question that is less likely to be true

An Object-Oriented Approach to Programming Logic and Design 66 Summary (continued) OR decision requires that only one of the two conditions is true to return a True result In an OR decision, first ask the question that is more likely to be true For range checks, make comparisons with either the lowest or highest value in each range

An Object-Oriented Approach to Programming Logic and Design 67 Summary (continued) Avoid unnecessary or previously answered questions case structure allows question with more than two alternatives Decision table lists conditions and combinations of outcomes