Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

Slides:



Advertisements
Similar presentations
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4: Selections.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1.
Control Structures Corresponds with Chapters 3 and 4.
James Tam Making Decisions In Python In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 3 Control Statements.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Control Structure.
The If/Else Statement, Boolean Flags, and Menus Page 180
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Running JavaScript Chapter 18.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 3 Selections.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
CMPE13Cyrus Bazeghi Chapter 13 C Control Structures.
Information  HW1 now available  Group Project (Find your group members)  Start thinking about group projects.
Flow of Control Part 1: Selection
Introduction to Control Statements JavaScript – Part 3 George Mason University June 3, 2010.
Chapter 4 Introduction to Control Statements Section 1 - Additional Java Operators Section 2 - If Statements Section 3 - If-Else Statements Section 4.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Marginal Tax Rate Single Married Filing Jointly or Qualified Widow(er) Married Filing Separately Head of Household 10%$0 – $8,350 $0 – $16,700.
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
Chapter 3 Selection Statements §3.1 The Boolean Type and Operators §3.2 The if-else Statements §3.3 Case Studies §3.4 Logical Operators §3.5 Switch Statements.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
1 Chapter 5 Control Statements. 2 Objectives F To understand the flow of control in selection and loop statements. F To use Boolean expressions to control.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
A First Book of C++ Chapter 4 Selection.
Introduction to Control Statements IT108 George Mason University.
Chapter 3 Selections Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved
Chapter 3 Selection Statements
Chapter 4 Selections © Copyright 2012 by Pearson Education, Inc. All Rights Reserved.
Group Project (Find your group members)
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Chapter 3 Selections Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Chapter 3 Control Statements
Line Continuation, Output Formatting, and Decision Structures
Selections Java.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Decisions Chapter 4.
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 13 Control Structures
Chapter 5 Control Statements
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1.
Line Continuation, Output Formatting, and Decision Structures
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Chapter 3 Control Statements
3. Decision Structures Rocky K. C. Chang 19 September 2018
CS2011 Introduction to Programming I Selections (II)
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Chapter 13 Control Structures
Presentation transcript:

Boolean Tricks

Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex

Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex Just because you can doesn't mean you should. Better:

% Check if x is a multiple of y: x % y == 0

Even/Odd x % 2 is: – 0  even – 1  odd

Comparing Floating Points According to C++: 3.02 != ( ) Do NOT compare floats directly with == or !=

Comparing Floating Points Compare if two floating points are "close enough" |num1 – num2| < threshold

Comparing Floating Points Compare if two floating points are "close enough" |num1 – num2| < threshold absolute value: abs(expression) – in threshold – depends on problem, have 15 significant digits to work with

Comparing Floating Points

Naked Booleans Boolean variable can be used as condition: Same as:

! ! is "not" Preferred to:

Problem Want to read in temperature and print "Temp is good" if it is between 68 and 72, or warning if it is too hot/cold.

Boolean Flags Flag variable : Boolean used to track condition – Start as true or false – Modify based on eveidence

Nested Ifs

Nested ifs Conditionals can be "nested"

If/Else Matching This code has a logic error:

If/Else Matching This code has a logic error:

If/Else Matching What computer saw:

If/Else Matching { } make ending of if/else explicit Editor will show brace matching Use indentation as visual clue

If/Else If can be the body of an else:

If/Else if… else if… else if… else form: – More compact/legible – Only one option is chosen

Bad If/Else A chain of plain if's not mutually exclusive!

Example: Computing Taxes The US federal personal income tax is calculated based on the filing status and taxable income. There are four filing statuses: single filers, married filing jointly, married filing separately, and head of household. Read in someone's status and income and determine their tax:

Conditions 2 Inputs – Marital status – $ earned 1 Output: tax

What happens? No matter what: – Get status – Get income – Print tax Conditionally: – What math to do

Step 1 Distinguish based on status

Step 2 Refine each category based on earnings: