Programming Logic and Design Fifth Edition, Comprehensive Chapter 4 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

Programming Logic and Design Eighth Edition
Understanding the Three Basic Structures
Objectives AND logic OR logic Evaluating compound conditions with multiple logical operators Precedence when combining AND and OR operators Efficiency.
Programming Logic and Design Seventh Edition
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure.
© 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
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
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
Programming Logic and Design Fifth Edition, Comprehensive
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Flow of Control Part 1: Selection
Selection Control Structure. Topics Review sequence control structure Structure theorem Selection control structure If statement Relational operators.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
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.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Chapter 5: Making Decisions
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
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.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding the Three Basic Structures Structure: a basic unit of programming logic Any program.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
Programming Logic and Design Fifth Edition, Comprehensive
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
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.
Java Programming Fifth Edition
The if…else Selection Statement
More on the Selection Structure
The Selection Structure
Topics The if Statement The if-else Statement Comparing Strings
An Introduction to Programming with C++ Fifth Edition
Topics The if Statement The if-else Statement Comparing Strings
Relational Operators Operator Meaning < Less than > Greater than
Boolean Expressions to Make Comparisons
Programming Logic and Design Fifth Edition, Comprehensive
The Selection Structure
Using C++ Arithmetic Operators and Control Structures
Presentation transcript:

Programming Logic and Design Fifth Edition, Comprehensive Chapter 4 Making Decisions

Programming Logic and Design, Fifth Edition, Comprehensive2 Objectives Evaluate Boolean expressions to make comparisons Use the relational comparison operators Learn about AND logic Learn about OR logic Make selections within ranges

Programming Logic and Design, Fifth Edition, Comprehensive3 Objectives (continued) Learn about precedence when combining AND and OR selections Learn more about the case structure Use a decision table

Programming Logic and Design, Fifth Edition, Comprehensive4 Evaluating Boolean Expressions to Make Comparisons Dual-alternative (or binary) selection structure: – Provides an action for each of two possible outcomes Figure 4-1 The dual-alternative selection structure

Programming Logic and Design, Fifth Edition, Comprehensive5 Evaluating Boolean Expressions to Make Comparisons (continued) Single-alternative (or unary) selection structure –Action is provided for only one outcome Figure 4-2 The single-alternative selection structure

Programming Logic and Design, Fifth Edition, Comprehensive6 Evaluating Boolean Expressions to Make Comparisons (continued) Figure 4-3 Flowchart and pseudocode for overtime payroll program

Programming Logic and Design, Fifth Edition, Comprehensive7 Evaluating Boolean Expressions to Make Comparisons (continued) Figure 4-4 Pseudocode for payroll program with dental insurance determination

Programming Logic and Design, Fifth Edition, Comprehensive8 Evaluating Boolean Expressions to Make Comparisons (continued) Boolean expression –Represents only one of two states –Evaluates to true or false Every decision in a computer program involves evaluating a Boolean expression Computer circuitry consists of two-state on-off switches –Represented by 1 or 0 Every computer decision yields a true-false result

Programming Logic and Design, Fifth Edition, Comprehensive9 Using the Relational Comparison Operators For any two values, three types of comparisons: –Two values are equal –First value greater than the second value –First value less than the second value Relational comparison operators –Express Boolean tests Different languages use different symbols

Programming Logic and Design, Fifth Edition, Comprehensive10 Using the Relational Comparison Operators (continued) Any logical situation can be expressed with only three types of comparisons: =, >, and < –Operators >= and <= are not necessary, but make code more readable “Not equal” operator –Most confusing of comparisons –Most likely to be different in different languages

Programming Logic and Design, Fifth Edition, Comprehensive11 Using the Relational Comparison Operators (continued) Figure 4-5 Using a negative comparison

Programming Logic and Design, Fifth Edition, Comprehensive12 Using the Relational Comparison Operators (continued) Figure 4-6 Using the positive equivalent of the negative comparison in Figure 4-5

Programming Logic and Design, Fifth Edition, Comprehensive13 Using the Relational Comparison Operators (continued) Table 4-1 Relational comparisons

Programming Logic and Design, Fifth Edition, Comprehensive14 Understanding AND Logic Compound condition –Asks multiple questions before an outcome is determined AND decision –Requires that both of two tests evaluate to True –Requires a nested decision (nested if) Using nested if statements – Second selection structure is contained entirely within one side of first structure – else clause paired with last if

Programming Logic and Design, Fifth Edition, Comprehensive15 Understanding AND Logic (continued) Figure 4-8 Flowchart and pseudocode for selection process in revised bonus- determining program

Programming Logic and Design, Fifth Edition, Comprehensive16 Nesting AND Decisions for Efficiency When nesting decisions, either selection can come first Performance time can be improved by asking questions in the proper order In an AND decision, first ask the question that is less likely to be true –Eliminates as many instances of the second decision as possible –Speeds up processing time

Programming Logic and Design, Fifth Edition, Comprehensive17 Combining Decisions in an AND Selection Conditional AND operator allows you to ask two or more questions in a single comparison Each Boolean expression must be true for entire expression to evaluate to true Truth tables describe the truth of an entire expression based on the truth of its parts Short-circuit evaluation: expression evaluated only as far as necessary to determine truth

Programming Logic and Design, Fifth Edition, Comprehensive18 Combining Decisions in an AND Selection (continued) Table 4-2 Truth table for the AND operator

Programming Logic and Design, Fifth Edition, Comprehensive19 Combining Decisions in an AND Selection (continued) Figure 4-10 Using an AND operator and the logic behind it

Programming Logic and Design, Fifth Edition, Comprehensive20 Avoiding Common Errors in an AND Selection Second decision must be made entirely within the first decision Range of values – every value between low and high limits In most programming languages logical AND is a binary operator –Requires complete Boolean expression on both sides

Programming Logic and Design, Fifth Edition, Comprehensive21 Understanding OR Logic When you want to take action when one or the other of two conditions is true Example: –Salespeople get bonus when they have achieved one of two goals: Sell at least five items Sell at least $2,000 in merchandise – itemsSold >= ITEMS_MIN ? If true, assign $300 bonus

Programming Logic and Design, Fifth Edition, Comprehensive22 Writing OR Decisions for Efficiency May ask either question first –Both produce the same output, but vary widely in number of questions asked If first question is true, no need to ask second In an OR decision first ask the question that is more likely to be true –Eliminates as many repetitions as possible of second decision

Programming Logic and Design, Fifth Edition, Comprehensive23 Combining Decisions in an OR Selection Conditional OR operator allows you to ask two or more questions in a single comparison Only one Boolean expression in an OR selection must be true to produce a result of true Question placed first will be asked first, so consider efficiency Computer can ask only one question at a time

Programming Logic and Design, Fifth Edition, Comprehensive24 Combining Decisions in an OR Selection (continued) Table 4-3 Truth table for the OR operator

Programming Logic and Design, Fifth Edition, Comprehensive25 Combining Decisions in an OR Selection (continued) Figure 4-14 Using an OR operator and the logic behind it

Programming Logic and Design, Fifth Edition, Comprehensive26 Avoiding Common Errors in an OR Selection Second question must be self-contained structure with one entry and exit point Request for A and B in English often translates to a request for A or B logically –Example: “Give a bonus to anyone who has sold at least three items and to anyone who has sold $2000” “Give a bonus to anyone who has sold at least three items or $2000”

Programming Logic and Design, Fifth Edition, Comprehensive27 Avoiding Common Errors in an OR Selection (continued) Figure 4-15 Unstructured flowchart for determining bonuses

Programming Logic and Design, Fifth Edition, Comprehensive28 Avoiding Common Errors in an OR Selection (continued) Figure 4-16 Incorrect logic that attempts to provide a discount for young and old movie patrons

Programming Logic and Design, Fifth Edition, Comprehensive29 Avoiding Common Errors in an OR Selection (continued) Figure 4-17 Correct logic that provides a discount for young and old movie patrons

Programming Logic and Design, Fifth Edition, Comprehensive30 Avoiding Common Errors in an OR Selection (continued) Figure 4-18 Incorrect logic that attempts to charge full price for patrons over 12 and under 65

Programming Logic and Design, Fifth Edition, Comprehensive31 Avoiding Common Errors in an OR Selection (continued) Figure 4-19 Correct logic that charges full price for patrons over 12 and under 65

Programming Logic and Design, Fifth Edition, Comprehensive32 Making Selections within Ranges Range check: compare a variable to a series of values between limits Use the lowest or highest value in each range Adjust the question logic when using highest versus lowest values Should end points of the range be included? –Yes: use >= or <= –No: use

Programming Logic and Design, Fifth Edition, Comprehensive33 Making Selections within Ranges (continued) Figure 4-20 Proposed meeting schedule

Programming Logic and Design, Fifth Edition, Comprehensive34 Making Selections within Ranges (continued) Figure 4-22 Program that prints the employee memo using high-end values in a range check

Programming Logic and Design, Fifth Edition, Comprehensive35 Understanding Common Errors Using Range Checks Avoid dead or unreachable paths –Don’t check for values that can never occur –Requires some prior knowledge of the data Never ask a question if there is only one possible outcome Avoid asking a question when the logic has already determined the outcome

Programming Logic and Design, Fifth Edition, Comprehensive36 Understanding Precedence When Combining AND and OR Selections Combine multiple AND and OR operators in an expression When multiple conditions must all be true, use multiple AND s if score1 >= 75 AND score2 >= 75 AND score 3 >= 75 then classGrade = “Pass” else classGrade = “Fail” endif

Programming Logic and Design, Fifth Edition, Comprehensive37 Understanding Precedence When Combining AND and OR Selections (continued) When only one of multiple conditions must be true, use multiple ORs if score1 >= 75 OR score2 >= 75 OR score3 >= 75 then classGrade = “Pass” else classGrade = “Fail” endif

Programming Logic and Design, Fifth Edition, Comprehensive38 Understanding Precedence When Combining AND and OR Selections (continued) When AND and OR operators are combined in the same statement, AND operators are evaluated first if age = 65 AND rating = “G” Use parentheses to correct logic and force evaluations to occur in the order desired if (age = 65) AND rating = “G”

Programming Logic and Design, Fifth Edition, Comprehensive39 Understanding Precedence When Combining AND and OR Selections (continued) Mixing AND and OR operators makes logic more complicated Can avoid mixing AND and OR decisions by nesting if statements

Programming Logic and Design, Fifth Edition, Comprehensive40 Understanding Precedence When Combining AND and OR Selections (continued) Figure 4-25 Nested decisions that determine movie patron discount

Programming Logic and Design, Fifth Edition, Comprehensive41 The Case Structure Provides a series of alternatives based on the value of a single variable Replaces a series of chained if-else statements Makes code easier to read

Programming Logic and Design, Fifth Edition, Comprehensive42 The Case Structure (continued) Figure 4-27 Flowchart and pseudocode that determines base price for house based on model number using the case structure

Programming Logic and Design, Fifth Edition, Comprehensive43 Using Decision Tables Managing multiple possible outcomes of multiple decisions can be difficult Decision table: four-part problem-analysis tool –Conditions –Possible combinations of Boolean values for each condition –Possible actions based on the conditions –Specific actions that correspond to each Boolean value of each condition

Programming Logic and Design, Fifth Edition, Comprehensive44 Using Decision Tables (continued) Figure 4-28 Sample residence hall assignments report

Programming Logic and Design, Fifth Edition, Comprehensive45 Using Decision Tables (continued) Rules for assigning residence halls –Students under age 21 who request a hall with quiet study hours: Addams Hall –Students under age 21 who do not request a hall with quiet study hours: Grant Hall –Students age 21 and over who request a hall with quiet study hours: Lincoln Hall –Students age 21 and over who do not request a hall with quiet study hours: Lincoln Hall

Programming Logic and Design, Fifth Edition, Comprehensive46 Using Decision Tables (continued) To create a decision table: –List all possible conditions –Determine the possible Boolean value combinations for each condition –# combinations = 2 (number of conditions) Table 4-4 Conditions and possible values for residence hall determination

Programming Logic and Design, Fifth Edition, Comprehensive47 Using Decision Tables (continued) To create a decision table (continued): –Add rows to list possible outcome actions –Choose one required outcome for each combination Table 4-6 Completed decision table for residence hall selection

Programming Logic and Design, Fifth Edition, Comprehensive48 Using Decision Tables (continued) Figure 4-29 Program that assigns a student to a residence hall

Programming Logic and Design, Fifth Edition, Comprehensive49 Summary Decisions involve evaluating Boolean expressions Use relational operators to compare values AND decision requires that both conditions be true to produce a true result In an AND decision, first ask the question that is less likely to be true OR decision requires that either of the conditions be true to produce a true result

Programming Logic and Design, Fifth Edition, Comprehensive50 Summary (continued) In an OR decision, first ask the question that is more likely to be true For a range check, make comparisons with the highest or lowest values in each range Eliminate unnecessary or previously answered questions Case structure allows a series of alternative actions based on the value in a single variable Decision table aids in program design analysis to manage multiple conditions and decisions