Programming Logic and Design Fourth Edition, Introductory

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

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.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
Chapter 4: Control Structures: Selection
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
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
- Meeting 5 – Making Decision By: Felix Valentin, MBA.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
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
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.
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 Control Structures Simple Program Design Third Edition A Step-by-Step Approach 4.
Chapter 4 Selection Structures: Making Decisions.
Programming Logic and Design, Second Edition, Comprehensive
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
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.
Decisions and Debugging Part06dbg --- if/else, switch, validating data, and enhanced MessageBoxes.
CHAPTER 4: Selection Control Structure. Objectives  Use the relational comparison operators  Learn about AND logic  Learn about OR logic  Make selections.
CPS120: Introduction to Computer Science Operations Lecture 9.
Programming with Microsoft Visual Basic th Edition
Chapter 5: Making Decisions
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.
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.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Chapter 4 Select … Case Multiple-Selection Statement & Logical Operators 1 © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon.
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)
Programming Logic and Design Fifth Edition, Comprehensive Chapter 4 Making Decisions.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
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
More on the Selection Structure
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 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Relational Operators.
Chapter 3: Selection Structures: Making Decisions
The Selection Structure
Using C++ Arithmetic Operators and Control Structures
Control Structures.
Presentation transcript:

Programming Logic and Design Fourth Edition, Introductory Chapter 5 Making Decisions

Objectives Evaluate Boolean expressions to make comparisons Use the relational comparison operators Understand AND logic Understand OR logic Use selections within ranges Programming Logic and Design, Introductory, Fourth Edition

Objectives (continued) Understand precedence when combining AND and OR selections Understand the case structure Use decision tables Programming Logic and Design, Introductory, Fourth Edition

Evaluating Boolean Expressions to Make Comparisons Dual-alternative (or binary) selection structure: Provides an action for each of two possible outcomes Programming Logic and Design, Introductory, Fourth Edition

Evaluating Boolean Expressions to Make Comparisons (continued) Single-alternative (or unary) selection structure Action is provided for only one outcome Programming Logic and Design, Introductory, Fourth Edition

Evaluating Boolean Expressions to Make Comparisons (continued) Dual-alternative (or binary) selection structure: Also called an if-then-else structure Programming Logic and Design, Introductory, Fourth Edition

Evaluating Boolean Expressions to Make Comparisons (continued) Single-alternative (or unary) selection structure Also called an if-then structure Programming Logic and Design, Introductory, Fourth Edition

Evaluating Boolean Expressions to Make Comparisons (continued) Represents only one of two states Expression evaluates to either true or false Expressions with relational operators produce Boolean results: hours worked > 40 Programming Logic and Design, Introductory, Fourth Edition

Using the Relational Comparison Operators Six possible ways to compare two values: Both are equal The first is greater than the second The first is less than the second The first is greater than or equal to the second The first is less than or equal to the second The two values are not equal Programming Logic and Design, Introductory, Fourth Edition

Using the Relational Comparison Operators (continued) To express Boolean tests when comparing values Different languages use different symbols Equals: = Less than: < Greater than: < Less than or equal: <= Greater than or equal: >= Programming Logic and Design, Introductory, Fourth Edition

Using the Relational Comparison Operators (continued) Any logical situation can be expressed with only three types of comparisons: =, >, and < >= and <= are not necessary, but make code more readable Adjust the logic based on the comparison type Programming Logic and Design, Introductory, Fourth Edition

Using the Relational Comparison Operators (continued) Rule of thumb: ask the question most likely to have a positive outcome Avoid “not equal” when it results in a double negative Programming Logic and Design, Introductory, Fourth Edition

Using the Relational Comparison Operators (continued) Rephrase in the positive Some comparisons are clearer when negative is used Programming Logic and Design, Introductory, Fourth Edition

Using the Relational Comparison Operators (continued) Programming Logic and Design, Introductory, Fourth Edition

Understanding AND Logic AND decision Requires that both of two tests evaluate to True Requires a nested decision (nested if) Programming Logic and Design, Introductory, Fourth Edition

Understanding AND Logic (continued) Developing the application The input data Programming Logic and Design, Introductory, Fourth Edition

Understanding AND Logic (continued) Developing the application The intended output: Programming Logic and Design, Introductory, Fourth Edition

Understanding AND Logic (continued) Developing the application The mainline logic: Programming Logic and Design, Introductory, Fourth Edition

Understanding AND Logic (continued) housekeeping() module: Programming Logic and Design, Introductory, Fourth Edition

Understanding AND Logic (continued) createReport() module: Programming Logic and Design, Introductory, Fourth Edition

Understanding AND Logic (continued) Programming Logic and Design, Introductory, Fourth Edition

Writing Nested AND Decisions for Efficiency For nested decisions, decide which to make first An appropriate choice may improve performance Consider expected outcomes to determine the most efficient way to nest the decisions Programming Logic and Design, Introductory, Fourth Edition

Writing Nested AND Decisions for Efficiency (continued) With 1000 employees, of which 90% (900) are in the medical plan, and 50% (500) are in the dental plan: First question is asked 1000 times Second question is asked 900 times Programming Logic and Design, Introductory, Fourth Edition

Writing Nested AND Decisions for Efficiency (continued) With 1000 employees, of which 90% (900) are in the medical plan, and 50% (500) are in the dental plan: First question is asked 1000 times Second question is asked 500 times Programming Logic and Design, Introductory, Fourth Edition

Writing Nested AND Decisions for Efficiency (continued) This analysis may not be possible: You may not know decision outcome likelihoods The decisions may not be mutually exclusive Rule of Thumb: First ask the question that is less likely to be true Reduces the number of times the second question will need to be asked Programming Logic and Design, Introductory, Fourth Edition

Combining Decisions in an AND Selection Logical AND operator: Allows you to ask two or more questions (Boolean expressions) in a single comparison Each Boolean expression in an AND selection must be true to produce a result of true Question placed first will be asked first, so consider efficiency Programming Logic and Design, Introductory, Fourth Edition

Combining Decisions in an AND Selection (continued) Programming Logic and Design, Introductory, Fourth Edition

Combining Decisions in an AND Selection (continued) Programming Logic and Design, Introductory, Fourth Edition

Avoiding Common Errors in an AND Selection Failure to nest 2nd decision entirely within 1st decision Programming Logic and Design, Introductory, Fourth Edition

Avoiding Common Errors in an AND Selection (continued) Incorrect questions to determine inclusion in a range Correct way: Programming Logic and Design, Introductory, Fourth Edition

Avoiding Common Errors in an AND Selection (continued) Failure to use two complete Boolean expressions: Correct way: Programming Logic and Design, Introductory, Fourth Edition

Understanding OR Logic OR decision At least one of two conditions must be true to produced a result of True If first condition is true, no need to test the second condition Programming Logic and Design, Introductory, Fourth Edition

Understanding OR Logic (continued) createReport() module: Programming Logic and Design, Introductory, Fourth Edition

Avoiding Common Errors in an OR Selection Unstructured OR selection: Programming Logic and Design, Introductory, Fourth Edition

Avoiding Common Errors in an OR Selection (continued) Incorrect interpretation of English: Casual use of AND when logic requires OR Programming Logic and Design, Introductory, Fourth Edition

Avoiding Common Errors in an OR Selection (continued) Correct logic: Programming Logic and Design, Introductory, Fourth Edition

Avoiding Common Errors in an OR Selection (continued) Incorrect interpretation of English Use of OR when AND logic is required Programming Logic and Design, Introductory, Fourth Edition

Avoiding Common Errors in an OR Selection (continued) Correct logic: Programming Logic and Design, Introductory, Fourth Edition

Writing OR Decisions for Efficiency How many decisions? Programming Logic and Design, Introductory, Fourth Edition

Writing OR Decisions for Efficiency (continued) Programming Logic and Design, Introductory, Fourth Edition

Writing OR Decisions for Efficiency (continued) Both produce the same output, but vary widely in number of questions asked If first question is true, no need to ask second Rule of thumb: First ask the question that is more likely to be true Programming Logic and Design, Introductory, Fourth Edition

Combining Decisions in an OR Selection Logical OR operator: Allows you to ask two or more questions (Boolean expressions) 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 Programming Logic and Design, Introductory, Fourth Edition

Combining Decisions in an OR Selection (continued) Using an OR operator: Programming Logic and Design, Introductory, Fourth Edition

Combining Decisions in an OR Selection (continued) What the computer actually does: Programming Logic and Design, Introductory, Fourth Edition

Using 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 < or > Programming Logic and Design, Introductory, Fourth Edition

Using Selections Within Ranges (continued) Using high-end values in the range check: Programming Logic and Design, Introductory, Fourth Edition

Using Selections Within Ranges (continued) Using low-end values in the range check: Programming Logic and Design, Introductory, Fourth Edition

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 Programming Logic and Design, Introductory, Fourth Edition

Common Errors Using Range Checks (continued) Programming Logic and Design, Introductory, Fourth Edition

Common Errors Using Range Checks (continued) Avoid asking an unneeded question If there is only one possible outcome If previous logic has already determined the answer Programming Logic and Design, Introductory, Fourth Edition

Common Errors Using Range Checks (continued) Programming Logic and Design, Introductory, Fourth Edition

Understanding Precedence When Combining AND and OR Selections Can combine multiple AND and OR operators in an expression When multiple conditions must all be true, use multiple ANDs Programming Logic and Design, Introductory, Fourth Edition

Understanding Precedence When Combining AND and OR Selections (continued) When only one of multiple conditions must be true, use multiple ORs Programming Logic and Design, Introductory, Fourth Edition

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 Use parentheses to correct logic and force evaluations to occur in the order desired Programming Logic and Design, Introductory, Fourth Edition

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, Introductory, Fourth Edition

Understanding Precedence When Combining AND and OR Selections (continued) Programming Logic and Design, Introductory, Fourth Edition

Understanding the Case Structure Used to provide a series of alternatives based on the value of a single variable Replaces a series of chained if-else statements May make the code easier to read Programming Logic and Design, Introductory, Fourth Edition

Understanding the Case Structure (continued) Programming Logic and Design, Introductory, Fourth Edition

Understanding the Case Structure (continued) Programming Logic and Design, Introductory, Fourth Edition

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, Introductory, Fourth Edition

Using Decision Tables (continued) Developing the application: The data Programming Logic and Design, Introductory, Fourth Edition

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, Introductory, Fourth Edition

Using Decision Tables (continued) Developing the application: The desired output Programming Logic and Design, Introductory, Fourth Edition

Using Decision Tables (continued) Programming Logic and Design, Introductory, Fourth Edition

Using Decision Tables (continued) Programming Logic and Design, Introductory, Fourth Edition

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) Programming Logic and Design, Introductory, Fourth Edition

Using Decision Tables (continued) To create a decision table (continued): Add rows to list possible outcome actions Programming Logic and Design, Introductory, Fourth Edition

Using Decision Tables (continued) To create a decision table (continued): Choose one required outcome for each combination Programming Logic and Design, Introductory, Fourth Edition

Using Decision Tables (continued) Create a flowchart from the decision table Draw a path for each column’s outcome Programming Logic and Design, Introductory, Fourth Edition

Using Decision Tables (continued) Resulting flowchart created directly from table has two identical outcomes: an unneeded question Programming Logic and Design, Introductory, Fourth Edition

Using Decision Tables (continued) Programming Logic and Design, Introductory, Fourth Edition

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, Introductory, Fourth Edition

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 Programming Logic and Design, Introductory, Fourth Edition