Chapter 5: Making Decisions

Slides:



Advertisements
Similar presentations
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Advertisements

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
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:
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
- 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
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.
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, 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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
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.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Programming with Microsoft Visual Basic th Edition
WRITING CONTROL STRUCTURES (CONDITIONAL CONTROL).
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.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Random Functions Selection Structure Comparison Operators Logical Operator
Programming Logic and Design Fifth Edition, Comprehensive Chapter 4 Making Decisions.
Java Programming Fifth Edition
More on the Selection Structure
EGR 2261 Unit 4 Control Structures I: Selection
The Selection Structure
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Decision Structures and Boolean Logic
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Decision Structures and Boolean Logic
Chapter 5: Control Structure
Boolean Expressions to Make Comparisons
Programming Logic and Design Fifth Edition, Comprehensive
Chapter 5 Decisions.
The Selection Structure
Using C++ Arithmetic Operators and Control Structures
Chapter 4: Decision Structures and Boolean Logic
Chapter 4: Writing and Designing a Complete Program
Control Structures.
Presentation transcript:

Chapter 5: Making Decisions Programming Logic and Design, Third Edition Introductory

Programming Logic and Design, Third Edition Introductory Objectives After studying Chapter 5, you should be able to: Evaluate Boolean expressions to make comparisons Use the logical comparison operators Understand AND logic Understand OR logic Programming Logic and Design, Third Edition Introductory

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

Evaluating Boolean Expressions to Make Comparisons The selection structure (sometimes called a decision structure) is one of the basic structures of structured programming Programming Logic and Design, Third Edition Introductory

Evaluating Boolean Expressions to Make Comparisons (continued) A dual-alternative, or binary, selection: has an action associated with each of two possible outcomes This selection structure is also called an if-then- else structure because it fits the statement: if the answer to the question is yes, then do something else do somethingElse endif Programming Logic and Design, Third Edition Introductory

Evaluating Boolean Expressions to Make Comparisons (continued) A single-alternative, or unary, selection action is required for only one outcome of the question A Boolean expression is one that represents only one of two states, usually expressed as true or false Every decision you make in a computer program involves evaluating a Boolean expression Programming Logic and Design, Third Edition Introductory

Using the Logical Comparison Operators Usually, you can compare only values that are of the same type: compare numeric values to other numeric values compare character values to other characters You can ask every programming question by using one of only three types of comparison operators in a Boolean expression Programming Logic and Design, Third Edition Introductory

Using the Logical Comparison Operators (continued) 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 In any Boolean expression, the two values used can be either variables or constants Each programming language supports its own set of logical comparison operators, or comparison symbols, that express these Boolean tests Programming Logic and Design, Third Edition Introductory

Using the Logical Comparison Operators (continued) Most languages allow you to use the algebraic signs for greater than (>) and less than (<) to make the corresponding comparisons Additionally, COBOL, which is very similar to English, allows you to spell out the comparisons in expressions like dayPastDue is greater than 30? or packageWeight is less than maximumWeightAllowed? RPG uses the two letter abbreviations GT and LT to represent greater than or less than Programming Logic and Design, Third Edition Introductory

Using the Logical Comparison Operators (continued) Most programming languages also provide for three additional comparisons 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. Any logical situation can be expressed using just three types of comparisons: equal, greater than, and less than Programming Logic and Design, Third Edition Introductory

Using the Logical Comparison Operators (continued) Comparing two amounts to decide if they are not equal to each other is the most confusing of all the comparisons Using “not equal to” in decisions involves thinking in double negatives, which makes you prone to include logical errors in your program Programming Logic and Design, Third Edition Introductory

Using the Logical Comparison Operators (continued) Programming Logic and Design, Third Edition Introductory

Using the Logical Comparison Operators (continued) Besides being awkward to use, the “not equal to” comparison operator is the one most likely to be different in various programming languages COBOL allows you to write “not equal to” Pascal uses a less-than sign followed immediately by a greater-than sign (<>) C#, C++, C and Java use an exclamation point followed by an equal sign (!=) Programming Logic and Design, Third Edition Introductory

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 Known as an AND decision 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 Programming Logic and Design, Third Edition Introductory

Understanding AND Logic (continued) A compound, or AND, decision requires a nested decision, or a nested if—that is, a decision “inside of” another decision Programming Logic and Design, Third Edition Introductory

Writing Nested 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 decision 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 Programming Logic and Design, Third Edition Introductory

Writing Nested AND Decisions for Efficiency (continued) In many AND decisions, you have no idea which of two events is more likely to occur; In that case, you can legitimately ask either question first In addition, even though you know the probability of each of two conditions, the two events might not be mutually exclusive In other words, one might depend on the other Programming Logic and Design, Third Edition Introductory

Combining Decisions in an AND Selection Most programming languages allow you to ask two or more questions in a single comparison by using a logical AND operator If the programming language you use allows an AND operator, you still must realize that the question you place first is the question that will be asked first cases that are eliminated based on the first question will not proceed to the second question Programming Logic and Design, Third Edition Introductory

Combining Decisions in an AND Selection (continued) The computer can ask only one question at a time Programming Logic and Design, Third Edition Introductory

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

Avoiding Common Errors in an AND Selection 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 Beginning programmers often make another type of error when they must make two comparisons on the same field while using a logical AND operator Programming Logic and Design, Third Edition Introductory

Understanding OR Logic Sometimes, you want to take action when one or the other of two conditions is true Called an OR decision because either one condition must be met or some other condition must be met, in order for an event to take place If someone asks you, “Are you free Friday or Saturday?,” only one of the two conditions has to be true in order for the answer to the whole question to be “yes” only if the answers to both halves of the question are false is the value of the entire expression false Programming Logic and Design, Third Edition Introductory

Understanding OR Logic (continued) Programming Logic and Design, Third Edition Introductory

Incorrect Flowchart for mainLoop() This flowchart is not allowed because it is not structured Programming Logic and Design, Third Edition Introductory

Avoiding Common Errors in an OR Selection An additional source of error that is specific to the OR selection stems from a problem with language The way we casually use English can cause an error when a decision based on a value falling within a range of values is required Programming Logic and Design, Third Edition Introductory

Writing OR Decisions for Efficiency You can write a program that creates a report containing all employees who have either the medical or dental insurance by using the mainLoop() Programming Logic and Design, Third Edition Introductory

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

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

Writing OR Decisions for Efficiency (continued) One of these selections is superior to the other Using either scenario, 950 employee records appear on the list, but the logic used in Figure 5- 26 requires 1,100 decisions, whereas the logic used in Figure 5-27 requires 1,500 decisions The general rule is: In an OR decision, first ask the question that is more likely to be true Programming Logic and Design, Third Edition Introductory

Combining Decisions in an OR Selection If you need to take action when either one or the other of two conditions is met, you can use two separate, nested selection structures, as in the previous examples However, most programming languages allow you to ask two or more questions in a single comparison by using a logical OR operator Programming Logic and Design, Third Edition Introductory

Using Selections Within Ranges Business programs often need to make selections based on a variable falling within a range of values When you use a range check, you compare a variable to a series of values between limits To perform a range check, make comparisons using either the lowest or highest value in each range of values you are using Programming Logic and Design, Third Edition Introductory

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-33 shows a range check in which the programmer has asked one question too many Another error that programmers make when writing the logic to perform a range check involves asking unnecessary questions Programming Logic and Design, Third Edition Introductory

Inefficient Range Selection Including Unreachable Path Programming Logic and Design, Third Edition Introductory

Understanding Precedence When Combining AND and OR Selections Most programming languages allow you to combine as many AND and OR operators in an expression as needed The logic becomes more complicated when you combine AND and OR operators within the same statement When you combine AND and OR operators, the AND operators take precedence, meaning their Boolean values are evaluated first You can avoid the confusion of mixing AND and OR decisions by nesting if statements instead of using ANDs and ORs Programming Logic and Design, Third Edition Introductory

Understanding the Case Structure The Logic below is completely structured Programming Logic and Design, Third Edition Introductory

Understanding the Case Structure (continued) Writing the logic using a case structure, as shown in Figure 5-37, might make it easier to understand The case structure provides a convenient alternative to using a series of decisions when you must make choices based on the value stored in a single variable When using the case structure, you test a variable against a series of values, taking appropriate action based on the variable’s value Programming Logic and Design, Third Edition Introductory

Flowchart and Pseudocode of Housing Model Using the Case Structure Programming Logic and Design, Third Edition Introductory

Programming Logic and Design, Third Edition Introductory 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 specific action that corresponds to each Boolean value of each condition Programming Logic and Design, Third Edition Introductory

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

Programming Logic and Design, Third Edition Introductory Summary (continued) Most programming languages allow you to ask two or more questions in a single comparison by using a logical AND operator An OR decision occurs when you want to take action when one or the other of two conditions is true Programming Logic and Design, Third Edition Introductory

Programming Logic and Design, Third Edition Introductory Summary (continued) Most programming languages allow you to ask two or more questions in a single comparison by using a logical OR operator The case structure provides a convenient alternative to using a series of decisions when you must make choices based on the value stored in a single variable Programming Logic and Design, Third Edition Introductory