Neal Stublen Human Decisions  Think about a decision you've made today?  How did you arrive at that decision?  Was it a black-and-white.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Control Structures.
Decisions with if statements. Simple If Statements if (age < 18) g.drawString(You may not vote., 50, 50); Evaluating a single expression.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
Chapter 4 - Control Statements
3-1 Chapter 3 Flow of Control (part a - branching)
A8 – Control Structures if, if-else, switch Control of flow in Java Any sort of complex program must have some ability to control flow.
The IF function Bernard Liengme. Objectives To know how to: Construct a condition using the comparison operators =, >=, >, ; Construct a formula using.
Making Decisions in Python Sec 9-10 Web Design. Objectives The student will: Understand how to make a decision in Python Understand the structure of an.
Selection (decision) control structure Learning objective
Objectives AND logic OR logic Evaluating compound conditions with multiple logical operators Precedence when combining AND and OR operators Efficiency.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
2-1 Making Decisions Sample assignment statements PayAmount = Hours * Rate PayAmount = 40*Rate + (Hours – 40)*Rate*1.5.
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.
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
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Computer Science Selection Structures.
Chapter 3 Making Decisions
CHAPTER 4: CONDITIONAL STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
CPS120: Introduction to Computer Science Decision Making in Programs.
Using Pre-written C Functions Need the following information –Name of the function –Types of the input parameters –Return type of the function Use the.
Selection Control Structures Simple Program Design Third Edition A Step-by-Step Approach 4.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
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
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 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
BACS 287 Programming Logic 2. BACS 287 Sequence Construct The sequence construct is the default execution mode for the CPU. The instructions are executed.
Chapter 5: Making Decisions
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
CPS120: Introduction to Computer Science Decision Making in Programs.
110 F-1 Decisions and Conditions Chapter 4: We can design a form We can calculate To make our programs more powerful, we need to be able to make choices.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.
CPS120: Introduction to Computer Science Decision Making in Programs.
5.02B Decision Making Structure (part 2). Compound Boolean Expressions.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
Random Functions Selection Structure Comparison Operators Logical Operator
IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators.
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.
Sequence, Selection, Iteration The IF Statement
Section 7.1 Logical Operators
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
Introduction To Robot Decision Making
Topics The if Statement The if-else Statement Comparing Strings
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Pages:51-59 Section: Control1 : decisions
Selection Statements.
Conditional Logic Presentation Name Course Name
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
CHAPTER 4: Conditional Structures
Chapter 3: Selection Structures: Making Decisions
Pages:51-59 Section: Control1 : decisions
Chapter 3: Selection Structures: Making Decisions
Presentation transcript:

Neal Stublen

Human Decisions  Think about a decision you've made today?  How did you arrive at that decision?  Was it a black-and-white decision for you?  Our decisions can be complex?

Computer "Decisions"  Computer decisions always reduce down to simple logic comparisons  Something is true or something is false  It has a boolean value of true or false  The decision block in a flow diagram requires a yes/no decision Is the light red? Is the light yellow? Is the light green?

Boolean Decisions  We use boolean expressions to determine a true or false condition  We implement a selection structure  Dual-alternative selection (if-then-else)  Single-alternative selection (if-then)

If-Then  If the condition is true, then perform an action or a sequence of actions if condition then action endif if time is 6pm then begin class endif

If-Then-Else  If the condition is true, then perform an action or a sequence of actions  Else perform a different action or sequence of actions if condition then action else another action endif if time is 6pm then begin class else wait endif

Boolean Expressions  An expression is just a statement the computer will evaluate x / 4  A boolean expression will be evaluated as either true or false  Relational operators are used in boolean expressions

Relational Operators  Equivalence, = or ==  Greater than, >  Less than, <  Greater than or equal to, >=  Less than or equal to, <=  Not equal to, <> or !=  Also called comparison operators

Relational Operators if customerAge >= 65 then discount = 0.10 else discount= 0.0 endif if customerAge <65 then discount = 0.0 else discount = 0.10 endif

Negation Operator  A negation operator reverses the logic of a true or false expression (NOT or !) if age >= 21 then allow purchase endif if NOT age >= 21 then refuse purchase endif

Example  What logic can we use to implement the following discount table: PurchaseDiscount $0.00 to $25.005% $25.01 to $ % $50.01 to $ % Over $ %

Implementing Minimal Conditions  Selecting from a group of ranges, the last check is never necessary  If the table is complete, we rule out one possibility with each check  If total is not > 100 and total is not > 50 and total is not > 25, then total must be <= 25.

Compound Conditions  Sometimes we need more complex logic to make a decision if I’m speeding then if I see a police car then slow down immediately endif

Nested Decisions if condition1 then if condition2 then take action endif if condition1 AND condition2 then take action endif

AND Operator if x AND y then do something endif “x AND y” is a boolean expression that can be evaluated as true or false xyx AND y True False TrueFalse

Short-Circuit Evaluation if x AND y then do something endif If we know x is false, we know x AND y is also false. There’s no need to evaluate y.

Short-Circuit Example if age > 12 AND age < 65 then movieDiscount = 0.0 endif If age is less than or equal to twelve, the computer will not need to determine if age is greater than 65.

Example  How would we implement the following discount table:  On Sundays, senior citizens receive an additional 5% discount. PurchaseDiscount $0.00 to $25.005% $25.01 to $ % $50.01 to $ % Over $ %

OR Operator if x OR y then do something endif “x OR y” is a boolean expression that can be evaluated as true or false xyx OR y True FalseTrue FalseTrue False

Short-Circuit Evaluation? if x OR y then do something endif Is there a short-circuit evaluation for the OR operator? If we know x is true, we know x OR y is also true. There’s no need to evaluate y.

OR Efficiency if age < 12 then movieDiscount = 0.10 endif if age > 65 then movieDiscount = 0.10 endif if age 65 then movieDiscount = 0.10 endif

AND/OR Precedence  AND operator is always evaluated before the OR operator  c1 OR c2 AND c3 OR c4  c1 OR (c2 AND c3) OR c4

Precedence Example if age = 65 AND not Friday then discount = 0.10 endif A twelve year old still gets the discount on Friday. if (age = 65) AND not Friday then discount = 0.10 endif Parentheses always clarify intention.

Summary  Boolean expressions  Relational operators  AND Logic  OR Logic  Selection within ranges  AND/OR precedence

Date Validation  What logic would we need to validate a user’s date input?  The user enters separate values for the month, day, and year.