Use Precedence Chart int number ; float x ; number ! = 0 && x < 1 / number / has highest priority < next priority != next priority && next priority What.

Slides:



Advertisements
Similar presentations
Logic & program control part 2: Simple selection structures.
Advertisements

Chapter 4: Making Decisions.
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 Chapter 5 Branching and Method Algorithm Design.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Multi-alternative Selection Both the if clause and the else clause of an if...else statement can contain any kind of statement, including another selection.
Single selection syntax if ( expression ) { statements; } TRUE FALSE expression if clause 1. Statemens are executed when expression is true 2. { } can.
1 Chapter 4 Selection and Encapsulation. 2 Chapter 4 Topics l Java Control Structures l boolean Data Type l Using Relational and Logical Operators in.
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures Dale/Weems/Headington.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures Dale/Weems.
Control Structures I (Selection)
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures.
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures.
Conditions, Logical Expressions, and Selection Control Structures Sumber dari :
Chapter 4 Logical Expressions & If-Else. 2 Overview  More on Data Type bool u Using Relational & Logical Operators to Construct & Evaluate Logical Expressions.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Conditions, logical expressions, and selection Introduction to control structures.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
Flow of Control There are 5 general types of Java control structures: Sequence (by default) Selection (also called branch or decision) Loop (also called.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Lecture no 3 Control statements.
Simple Control Structures IF, IF-ELSE statements in C.
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
COSC175-Selection1 Decisions Given hours worked and pay rate, calculate total pay What if you work overtime? How do you indicate if your work overtime?
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
1 Programming in C++ Dale/Weems/Headington Chapter 5 Conditions, Logical Expressions.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures Dale/Weems.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
1 Conditions, Logical Expressions, and Selection Control Structures.
Control statements Mostafa Abdallah
Lesson thirteen Conditional Statement "if- else" ©
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Selection in C++ If statements. Control Structures Sequence Selection Repetition Module.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
C++ Programming Control Structures I (Selection).
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 5 Topics Data Type bool
Sequence, Selection, Iteration The IF Statement
Chapter 4: Making Decisions.
Selections Java.
Chapter 4: Making Decisions.
A mechanism for deciding whether an action should be taken
Decisions Given hours worked and pay rate, calculate total pay
Operator Precedence Operators Precedence Parentheses () unary
Decisions Given hours worked and pay rate, calculate total pay
Conditionals & Boolean Expressions
Conditionals & Boolean Expressions
Topics Data Type bool Using Relational and Logical Operators to Construct and Evaluate Logical Expressions If-Then-Else Statements If-Then Statements Nested.
Relational Operators Operator Meaning < Less than > Greater than
Presentation transcript:

Use Precedence Chart int number ; float x ; number ! = 0 && x < 1 / number / has highest priority < next priority != next priority && next priority What happens if Number has value 0? Run Time Error (Division by zero) occurs.

Short-Circuit Benefits one Boolean expression can be placed first to “guard” a potentially unsafe operation in a second Boolean expression time is saved in evaluation of complex expressions using operators || and &&

Our Example Revisited int number; float x; ( number ! = 0) && ( x < 1 / number ) is evaluated first and has value false Because operator is &&, the entire expression will have value false. Due to short-circuiting the right side is not evaluated in C++.

WARNING about Expressions in C++ “Boolean expression” means an expression whose value is true or false each expression has a value this can lead to UNEXPECTED RESULTS use of parentheses is encouraged otherwise, use precedence chart to determine order

What went wrong? This is only supposed to display “HEALTHY AIR” if the air quality index is between 50 and 80. But when you tested it, it displayed “HEALTHY AIR” when the index was 35. int AQIndex ; AQIndex = 35 ; if (50 < AQIndex < 80) cout << “HEALTHY AIR“ ;

Analysis of Situation AQIndex = 35; According to the precedence chart, the expression (50 < AQIndex < 80) means (50 < AQIndex) < 80 because < is Left Associative (50 < AQIndex) is false (has value 0) (0 < 80) is true.

Corrected Version int AQIndex ; AQIndex = 35 ; if ( (50 < AQIndex) && (AQIndex < 80) ) cout << “HEALTHY AIR“ ;

Single selection syntax if ( expression ) { statements; } TRUE FALSE expression if clause 1. Statemens are executed when expression is true 2. { } can be omitted if only one staement

if ( Expression ) StatementA else StatementB NOTE: StatementA and StatementB each can be a single statement, a null statement, or a block. Double selection Syntax

if... else provides two-way selection between executing one of 2 clauses (the if clause or the else clause) TRUE FALSE expression if clauseelse clause

Use of blocks recommended if ( Expression ) { } else { } “if clause” “else clause”

int carDoors, driverAge ; float premium, monthlyPayment ;... if ( (carDoors == 4 ) && (driverAge > 24) ) { premium = ; cout << “ LOW RISK “ ; } else { premium = ; cout << “ HIGH RISK ” ; } monthlyPayment = premium / ;

What happens if you omit braces? if ( (carDoors == 4 ) && (driverAge > 24) ) premium = ; cout << “ LOW RISK “ ; else premium = ; cout << “ HIGH RISK ” ; monthlyPayment = premium / ; COMPILE ERROR OCCURS. The “if clause” is the single statement following the if.

Braces can only be omitted when each clause is a single statement if ( lastInitial <= ‘K’ ) volume = 1; else volume = 2; cout << “Look it up in volume # “ << volume << “ of NYC phone book”;

If-Else for a mail order Assign value.25 to discountRate and assign value to shipCost if purchase is over Otherwise, assign value.15 to discountRate and assign value 5.00 to shipCost Either way, calculate totalBill

These braces cannot be omitted if ( purchase > ) { discountRate =.25 ; shipCost = ; } else { discountRate =.15 ; shipCost = 5.00 ; } totalBill = purchase * (1.0 - discountRate) + shipCost ;