Programming Logic and Design Fourth Edition, Comprehensive 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
Understanding the Three Basic Structures
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture two Dr. Hamdy M. Mousa.
Objectives AND logic OR logic Evaluating compound conditions with multiple logical operators Precedence when combining AND and OR operators Efficiency.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
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
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
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.
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.
Programming Logic and Design, Second 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.
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.
CPS120: Introduction to Computer Science Operations Lecture 9.
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.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding the Three Basic Structures Structure: a basic unit of programming logic Any program.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
Programming Logic and Design Fifth Edition, Comprehensive
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.
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.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Java Programming Fifth Edition
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
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Topics The if Statement The if-else Statement Comparing Strings
Understanding the Three Basic Structures
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Three Special Structures – Case, Do While, and Do Until
Chapter 5: Control Structure
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 3: Selection Structures: Making Decisions
The Selection Structure
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Control Structures.
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Presentation transcript:

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

Programming Logic and Design, Introductory, Fourth Edition2 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 Edition3 Objectives (continued) Understand precedence when combining AND and OR selections Understand the case structure Use decision tables

Programming Logic and Design, Introductory, Fourth Edition4 Evaluating Boolean Expressions to Make Comparisons (continued) Boolean expression –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 Edition5 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 Edition6 Using the Relational Comparison Operators (continued) Relational comparison operators: –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: >= –Not equal: <>

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

Programming Logic and Design, Introductory, Fourth Edition8 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 Edition9 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 Edition10 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 Edition11 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 Edition12 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 Edition13 IF customerAge >= 65 THEN discount = 0.10 ELSE discount = 0 ENDIF IF customerAge < 65 THEN discount = 0 ELSE discount = 0.10 ENDIF IF customerAge < 64 THEN discount = 0 ELSE discount = 0.10 ENDIF Using the Relational Comparison Operators (continued) Each calculates a discount of 10% only when the customer age is 65 years old or greater ALL ARE CORRECT – THERE ARE NO WRONG WAYS!

Programming Logic and Design, Introductory, Fourth Edition14 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 Edition15 Using the Relational Comparison Operators (continued) Rephrase in the positive Some comparisons are clearer when negative is used

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

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

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

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

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

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

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

Programming Logic and Design, Introductory, Fourth Edition23 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 Edition24 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 (once for each employee) –Second question is asked 500 times ( there are only 500 in dental plan therefore only 500 “yes” answers) YES path NO path and completion of YES path YES path

Programming Logic and Design, Introductory, Fourth Edition25 Writing Nested AND Decisions for Efficiency (continued) Rule of Thumb: First ask the question that is less likely to be true – more likely to be false (first false terminates the question) –Reduces the number of times the second question will need to be asked

Programming Logic and Design, Introductory, Fourth Edition26 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 Edition27 Combining Decisions in an AND Selection (continued) Single IF statement

Programming Logic and Design, Introductory, Fourth Edition28 Combining Decisions in an AND Selection (continued) Multiple IF statement

Programming Logic and Design, Introductory, Fourth Edition29 Avoiding Common Errors in an AND Selection Failure to nest 2 nd decision entirely within 1 st decision Tests each decision separately no matter the outcome of the other decision.

Programming Logic and Design, Introductory, Fourth Edition30 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 Edition31 Understanding OR Logic (continued) createReport() module: IF empMedicalIns = Y OR empDentalIns = “Y” THEN print empIdNumber, empLastName, empFirstName ENDIF

Programming Logic and Design, Introductory, Fourth Edition32 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 Edition33 Avoiding Common Errors in an OR Selection (continued) Correct logic:

Programming Logic and Design, Introductory, Fourth Edition34 Avoiding Common Errors in an OR Selection (continued) Incorrect interpretation of English –Use of OR when AND logic is required < 65: All patrons (including 12 and below) > 12: All patrons (including 65 and above)

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

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

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

Programming Logic and Design, Introductory, Fourth Edition38 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 (first true will generate the TRUE path and the rest of the ORs will be ignored)

Programming Logic and Design, Introductory, Fourth Edition39 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 Edition40 Combining Decisions in an OR Selection (continued) Using an OR operator:

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

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

Programming Logic and Design, Introductory, Fourth Edition43 Range Selection Range: to get TRUE Task: print if empRate is 10 or 11

Programming Logic and Design, Introductory, Fourth Edition44 SYNTAX error Range Selection Good

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

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

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

Programming Logic and Design, Introductory, Fourth Edition48 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 Edition49 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 Edition50 Understanding Precedence When Combining AND and OR Selections (continued) AND OR

Programming Logic and Design, Introductory, Fourth Edition51 Understanding the Case Structure Used to provide a series of alternatives (IF statements) 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 Edition52 Understanding the Case Structure (continued)

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

Programming Logic and Design, Introductory, Fourth Edition54 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 Edition55 Using Decision Tables (continued) Developing the application: –The data

Programming Logic and Design, Introductory, Fourth Edition56 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 Edition57 Using Decision Tables (continued) Developing the application: –The desired output

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

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

Programming Logic and Design, Introductory, Fourth Edition60 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) –# of possible outcomes = 4 (residence halls) Addams Hall Grant Hall Lincoln Hall

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

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

Programming Logic and Design, Introductory, Fourth Edition63 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 Edition64 Using Decision Tables (continued) Resulting flowchart created directly from table has two identical outcomes: an unneeded question

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