Checking Multiple Conditions

Similar documents
C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 4: Control Structures I (Selection)

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Chapter 4: Control Structures I (Selection)

C++ Programming: From Problem Analysis to Program Design, Third Edition

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures

Programming Logic and Design Sixth Edition

Control Structures. A program can proceed: Sequentially Selectively (branch) - making a choice Repetitively (iteratively) - looping

Programming: Java. Chapter Objectives. Control Structures. Chapter 4: Control Structures I. Program Design Including Data Structures

C++ Program Flow Control: Selection

Computer Science & Engineering 150A Problem Solving Using Computers

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

5. Selection: If and Switch Controls

Control Structures: The IF statement!

Chapter 4 - Notes Control Structures I (Selection)

Lecture 5. Review from last week. Selection Statements. cin and cout directives escape sequences

Recognize the correct ordering of decisions in multiple branches Program simple and complex decision

Control Structures. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.

Chapter 7 Control Statements Continued

Boolean evaluation and if statements. Making decisions in programs

Text Input and Conditionals

UNIT-4 Black Box & White Box Testing

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

UNIT-4 Black Box & White Box Testing

Boolean Expressions and if 9/14/2007

Chapter 3, Selection. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved.

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

QUIZ: What value is stored in a after this

If Control Construct

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

Chapter 4: Making Decisions

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Chapter 5 Selection Statements. Mr. Dave Clausen La Cañada High School

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

Chapter 4: Making Decisions

Decisions. Arizona State University 1

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

INTRODUCTION TO COMPUTER SCIENCE - LAB

Introduction to Programming Using Java (98-388)

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Decision Structures. Selection. Selection options (in Java) Plain if s (3 variations) Each action could be any of: if else (3 variations)

Lecture 3 Tao Wang 1

CS1150 Principles of Computer Science Boolean, Selection Statements

Topics. Chapter 5. Equality Operators

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme

LECTURE 04 MAKING DECISIONS

EECS 183. Week 3 - Diana Gage. www-personal.umich.edu/ ~drgage

Boolean Algebra Boolean Algebra

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

C/C++ Programming Lecture 7 Name:

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Logical Operators and if/else statement. If Statement. If/Else (4.3)

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

5. Control Statements

In this chapter, you will:

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

COMP Primitive and Class Types. Yi Hong May 14, 2015

Fundamentals of Programming Session 9

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Chapter 4: Control Structures I

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

Unit 3. Operators. School of Science and Technology INTRODUCTION

Lecture 5 Tao Wang 1

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started

Selections. CSE 114, Computer Science 1 Stony Brook University

Linear execution of statements. CISC 1600/1610 Computer Science I. Alternatives to linear execution. Linear execution of statements

C/C++ Programming for Engineers: Matlab Branches and Loops

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

VARIABLES & ASSIGNMENTS

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

CSc 110, Autumn Lecture 11: if / else. Adapted from slides by Marty Stepp and Stuart Reges

The following expression causes a divide by zero error:

Announcements. Homework 0: using cin with 10/3 is NOT the same as (directly)

Problem Solving and Algorithms

Selection the If Statement Try Catch and Validation

Increment and the While. Class 15

1 Conditions and control statements. 1.1 Conditional statement if

Lesson 39: Conditionals #3 (W11D4)

Chapter 4: Basic C Operators

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

BITG 1113: POINTER LECTURE 12

BRANCHING if-else statements

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Using Boolean Expressions. Multiway Branches. More about C++ Loop Statements. Designing Loops. In this chapter, you will learn about:

Absolute C++ Walter Savitch

Prof. Navrati Saxena TA: Rochak Sachan

Chapter 3. More Flow of Control

COSC 122 Computer Fluency. Programming Basics. Dr. Ramon Lawrence University of British Columbia Okanagan

ECE 545 Lecture 5. Data Flow Modeling in VHDL. George Mason University

CS150 Introduction to Computer Science 1. Logical Operators and if/else statement

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

IS 0020 Program Design and Software Tools

LESSON 3 CONTROL STRUCTURES

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Transcription:

Checking Multiple Conditions Conditional code often relies on a value being between two other values Consider these conditions: Free shipping for orders over $25 10 items or less Children ages 3 to 11 allowed on play equipment What is the appropriate logical expression for each?

Checking Multiple Conditions Conditional code often relies on a value being between two other values Consider these conditions: Free shipping for orders over $25 order_total > 25.0 10 items or less itemcount <= 10 Children ages 3 to 11 allowed on play equipment age >= 3 and age <= 11 In the third example, we need a new operator to combine two logical expressions into one

Complex Logical Expressions Logical (Boolean) operators enable you to combine logical expressions unary binary binary C++ Programming: Program Design Including Data Structures, Fourth Edition 3

Logical (Boolean) Operators C++ Programming: Program Design Including Data Structures, Fourth Edition 6

Short-Circuit Evaluation Short-circuit evaluation: evaluation of a logical expression stops as soon as the value of the expression is known Example: (age >= 21) ( x == 5) //Line 1 (grade == 'A') && (x >= 7) //Line 2 C++ Programming: Program Design Including Data Structures, Fourth Edition 7

Range Checking Logical operators can be used to make arbitrarily complex expressions E.g. (x > 3) && (y <= 45) (x == 15) && (y < x) etc But checking to see if a number is between two others is one of the most common

Order of Precedence Relational and logical operators are evaluated from left to right The associativity is left to right Parentheses can override precedence C++ Programming: Program Design Including Data Structures, Fourth Edition 9

Order of Precedence (continued) C++ Programming: Program Design Including Data Structures, Fourth Edition 10

Order of Precedence (continued) C++ Programming: Program Design Including Data Structures, Fourth Edition 11

Order of Precedence (continued) C++ Programming: Program Design Including Data Structures, Fourth Edition 12

Order of Precedence (continued) C++ Programming: Program Design Including Data Structures, Fourth Edition 13

Common Error The variable x is equal to 4 or 5: Incorrect: x == 4 5 Correct: (x == 4) (x == 5) The former sounds right in English, so is very common mistake According to the order of precedence, it is evaluated as: (x == 4) 5 Which is always true (because 5 is always true)

How would: x == (4 5) Be evaluated? Common Error

Common Error: = vs. == C++ allows you to use any expression that can be evaluated to either true or false as an expression in the if statement: if (x = 5) cout << "The value is five." << endl; Very difficult mistake to catch It is not a syntax error It is a logical error C++ Programming: Program Design Including Data Structures, Fourth Edition 16

Floating-Point Equality Comparison of floating-point numbers for equality may not behave as you would expect Example: 1.0 == 3.0/7.0 + 2.0/7.0 + 2.0/7.0 evaluates to false Why? 3.0/7.0 + 2.0/7.0 + 2.0/7.0 = 0.99999999999999989 Solution: use a tolerance value Example: fabs(x y) < 0.000001 C++ Programming: Program Design Including Data Structures, Fourth Edition 17

Car Insurance Example Determine the policy premium based on the following rules: Gender Age Annual Premium Male Under 21 1500 + 200 for every ticket on record Male 21 to 29 1200 + 100 for every ticket on record Male 30 and older 1000 + 100 for every ticket on record Female Under 21 1200 + 200 for every ticket on record Female 21 or older 1000 + 100 for every ticket on record Five conditions with five outcomes

Design Questions What are the five conditions? Are they mutually exclusive? Do they cover all possibilities? What are the five outcomes that go along with those conditions?

From Design to Code Conditions and outcomes tell us what variables we need: gender, age, premium, tickets Five conditions = five branches in an if else tree Five outcomes = the bodies of those branches if( (gender == M ) && (age < 21) ) { premium = 1500 + (200 * tickets); } else if( )

Alternate Design Instead of dividing the people into 5 groups: Divide into two groups first (by gender) Then divide those into groups (by age) This results in a set of nested conditions gender == M age < 21 (age >= 21) && (age < 30) age >= 30 gender == F age < 21 age >= 21 Which can be converted into nested if else trees