Programming Logic and Design, Second Edition, Comprehensive

Slides:



Advertisements
Similar presentations
Project 6: Working with If Statements Essentials for Design JavaScript Level One Michael Brooks.
Advertisements

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
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.
Decision Structures Chapter 4. Chapter 4 Objectives To understand: o What values can be stored in a Boolean variable o What sequence structures are and.
Programming Logic and Design Eighth Edition
Selection (decision) control structure Learning objective
Programming Logic and Design Sixth Edition
Objectives AND logic OR logic Evaluating compound conditions with multiple logical operators Precedence when combining AND and OR operators Efficiency.
Programming Logic and Design, Third Edition Comprehensive
Objectives In this chapter, you will learn about:
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Writing a Complete Program
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Chapter 4: Control Structures: Selection
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
- Meeting 5 – Making Decision By: Felix Valentin, MBA.
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
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
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
Selection Control Structures Simple Program Design Third Edition A Step-by-Step Approach 4.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
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.
© Jalal Kawash Programming Peeking into Computer Science 1.
Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Chapter 5: Making Decisions
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
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.
2 Chapter 21 Understanding Structure Programming Logic and Design, Second Edition, Comprehensive 2.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
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.
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.
Java Programming Fifth Edition
More on the Selection Structure
The Selection Structure
Topics The if Statement The if-else Statement Comparing Strings
Topics The if Statement The if-else Statement Comparing Strings
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Chapter 4: Decision Structures and Boolean Logic
Chapter 8: More on the Repetition Structure
Chapter 5: Control Structure
Writing a Complete Program
Boolean Expressions to Make Comparisons
Programming Logic and Design Fifth Edition, Comprehensive
Chapter 5 Decisions.
Using C++ Arithmetic Operators and Control Structures
Chapter 4: Decision Structures and Boolean Logic
Chapter 4: Writing and Designing a Complete Program
Presentation transcript:

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

Objectives After studying Chapter 5, you should be able to: Evaluate Boolean expressions to make comparisons Use the logical comparison operators Understand AND logic Write AND decisions for efficiency Combine decisions in an AND situation Avoid common errors in an AND situation Chapter 5

Objectives After studying Chapter 5, you should be able to: Understand OR logic Avoid common errors in an OR situation Write OR decisions for efficiency Combine decisions in an OR situation Use selections within ranges Understand common errors using range checks Use decision tables Chapter 5

Evaluating Boolean Expressions to Make Comparisons The selection structure (sometimes called a decision structure) involved in programs is one of the basic structures of structured programming You can refer to the structure in Figure 5-1 as a dual-alternative or binary selection because there are two possible outcomes: depending on the answer to the question represented by the diamond, the logical flow proceeds either to the left branch of the structure or to the right Chapter 5

Evaluating Boolean Expressions to Make Comparisons Chapter 5

Evaluating Boolean Expressions to Make Comparisons This selection structure also called an if-then-else structure because it fits the statement The flowchart segment in Figure 5-2 represents a single-alternative or unary selection where action is required for only one outcome of the question You call this form of the if-then-else structure an if-then, because no “else” action is necessary Chapter 5

Flowchart and Pseudocode for Overtime Decision Chapter 5

Flowchart and Pseudocode for Dental Plan Decision Chapter 5

Evaluating Boolean Expressions to Make Comparisons The typical if-then decision in Figure 5-4 shows the employee’s paycheck being reduced if the employee participates in the dental plan No action is taken if the employee is not in the dental plan A Boolean expression is one that represents only one of two states, usually expressed as true or false Chapter 5

Using Logical Comparison Operators Usually, you can compare only values that are of the same type; that is, you can compare numeric values to other numeric values and character values to other characters For any two values that are the same type, you can decide whether: The two values are equal The first value is greater than the second value The first value is less than the second value Chapter 5

Using Logical Comparison Operators In any Boolean expression, the two values used can be either variables or constants Such expressions are considered trivial because each always results in the same value: true for the first expression and false for the second Each programming language supports its own set of logical comparison operators, or comparison symbols, that express these Boolean tests Chapter 5

Using Logical Comparison Operators In addition to the three basic comparisons you can make, most programming languages provide three others For any two values that are the same type, you can decide whether: The first is greater than or equal to the second The first is less than or equal to the second The two are not equal Chapter 5

Using Logical Comparison Operators Most programming languages allow you to express “greater than or equal to” by typing a greater-than sign immediately followed by an equal sign (>=) Any logical situation can be expressed using just three types of comparisons: equal, greater than, and less than In Figure 5-5, if the value of customerCode is equal to 1, the logical flow follows the false branch of the selection Chapter 5

Using a Negative Comparison Chapter 5

Using the Positive Equivalent of the Negative Comparison in Figure 5-5 Chapter 5

Using Logical Comparison Operators Besides being awkward to use, the not equal to comparison is the one most likely to be different in the various programming languages you may use Although NOT comparisons can be awkward to use, there are times when your meaning is clearest if you use one Chapter 5

Understanding AND Logic Often you need more than one selection structure to determine whether an action should take place For example, suppose that your employer wants a report that lists workers who have registered for both insurance plans offered by the company: the medical plan and the dental plan This type of situation is known as an AND situation because the employee’s record must pass two tests—participation in the medical plan and participation in the dental plan—before you write that employee’s information on the report Chapter 5

Understanding AND Logic An AND situation requires a nested decision or a nested if; that is, a decision “inside of” another decision Chapter 5

Understanding AND Logic Chapter 5

Flowchart of Mainline Logic for Medical and Dental Participant Report Chapter 5

Flowchart of Housekeeping() Routine for Medical and Dental Participant Chapter 5

mainLoop() of Program that Lists Medical and Dental Participants Chapter 5

Pseudocode of Medical and Dental Participants Report Program Chapter 5

Writing AND Decisions for Efficiency When you nest decisions because the resulting action requires that two conditions be true, you must decide which of the two decisions to make first Logically, either selection in an AND situation can come first However, when there are two selections, you often can improve your program’s performance by making an appropriate choice as to which selection to make first Chapter 5

Writing AND Decisions for Efficiency Chapter 5

Combining Decisions in an AND Situation Most programming languages allow you to ask two or more questions in a single comparison by using a logical AND operator If you want to select employees who carry both medical and dental insurance, you can use nested ifs, or you can include both decisions in a single statement by writing empDentalIns = “Y” AND empMedicalIns = “Y”? Chapter 5

Combining Decisions in an AND Situation Chapter 5

Avoiding Common Errors in an AND Situation When you must satisfy two or more criteria to initiate an event in a program, you must make sure that the second decision is made entirely within the first decision Chapter 5

Understanding OR Logic Sometimes you want to take action when one or the other of two conditions is true This is called an OR situation because either one condition must be met or some other condition must be met in order for an event to take place Chapter 5

Flowchart and Pseudocode for mainLoop() that Prints Employees Who Have Medical or Dental Insurance Chapter 5

Avoiding Common Errors in an OR Situation Logically, you can argue that the flowchart in Figure 5-21 is correct because the correct employees print However, this flowchart is not allowed because it is not structured Chapter 5

Writing OR Decisions for Efficiency You can write a program that creates a report containing all employees who take either the medical or dental insurance by using the mainLoop() in either Figure 5-22 or Figure 5-23 When you use the logic shown in Figure 5-22 to select employees who participate in either insurance plan, you first ask about medical insurance If you use Figure 5-23, you ask empDentalIns = “Y”? Chapter 5

Writing OR Decisions for Efficiency Using either scenario, 950 employee records appear on the list, but the logic used in Figure 5-22 requires 1,100 decisions while the logic used in Figure 5-23 require 1,500 decisions Chapter 5

Alternate mainLoop() to Select Employees with Medical or Dental Insurance Chapter 5

Combining Decisions in an OR Situation Most programming languages allow you to ask two or more questions in a single comparison by using a logical OR operator When you use the logical OR operator, only one of the listed conditions must be met for the resulting action to take place Chapter 5

Computer Logic of Program Containing an OR Decision Using an OR Operator Chapter 5

Using Selections within Ranges Business programs often need to make selections based on a variable falling within a range of values To perform a range check, make comparisons using either the lowest or highest values in each range of values you are using to make selections The pseudocode representing the logic for choosing a supervisor name by using the high-end values appears in Figure 5-27 Chapter 5

Using Selections within Ranges Chapter 5

Using Low-End Values for a Range Check Chapter 5

Common Errors Using Range Checks Two common errors that occur when programmers perform range checks both entail doing more work than is necessary Figure 5-29 shows a range check in which the programmer has asked one question too many Similarly, Figure 5-30 shows the beginning of an inefficient range selection Chapter 5

Inefficient Range Selection Chapter 5

Partial Example of Inefficient Range Selection Chapter 5

Using Decision Tables A decision table is a problem-analysis tool that consists of four parts: Conditions Possible combinations of Boolean values for the conditions Possible actions based on the conditions The actions that correspond to each Boolean value of each condition Chapter 5

Flowchart of Mainline Logic for Residence Hall Report Chapter 5

Flowchart of getReady() Module for Residence Hall Report Chapter 5

Using Decision Tables Chapter 5

Using Decision Tables Chapter 5

Decision Table for Residence Hall Selection, Part 3 of 3 Chapter 5

Flowchart for Residence Hall Selection, Part 1 of 5 Chapter 5

Flowchart for Residence Hall Selection, Part 2 of 5 Chapter 5

Flowchart for Residence Hall Selection, Part 3 of 5 Chapter 5

Flowchart for Residence Hall Selection, Part 4 of 5 Chapter 5

Flowchart for Residence Hall Selection, Part 5 of 5 Chapter 5

Summary Every decision you make in a computer program involves evaluating a Boolean expression For any two values that are the same type, you can use logical comparison operators to decide whether the two values are equal, the first value is greater than the second value, or the first value is less than the second value An AND situation occurs when two conditions must be true in order for a resulting action to take place Chapter 5

Summary An AND situation requires a nested decision or a nested if In an AND situation, first ask the question that is less likely to be true Most programming languages allow you to ask two or more questions in a single comparison by using a logical AND operator When you must satisfy two or more criteria to initiate an event in a program, you must make sure that the second decision is made entirely within the first decision, and that you use a complete Boolean expression on both sides of the AND Chapter 5

Summary An OR situation occurs when you want to take action when one or the other of the two conditions is true Errors occur in OR situations when programmers do not maintain structure In an OR situation, first ask the question that is more likely to be true Most programming languages allow you to ask two or more questions in a single comparison by using a logical OR operator Chapter 5

Summary To perform a range check, make comparisons with either the lowest or highest value in each range of values you are using Common errors that occur when programmers perform range checks include asking unnecessary and previously answered questions A decision table is a problem-analysis tool that consists of conditions, possible combinations of Boolean values for the conditions, possible actions based on the conditions, and the actions that correspond to each Boolean value of each condition Chapter 5