1 Programming in C++ Dale/Weems/Headington Chapter 5 Conditions, Logical Expressions.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Logic & program control part 2: Simple selection structures.
Chapter 4: Making Decisions.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
If Statements & Relational Operators Programming.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
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)
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Flow of Control is Sequential unless a “control structure” is used to change that there are 2 general types of control structures: Selection (also called.
Use Precedence Chart int number ; float x ; number ! = 0 && x < 1 / number / has highest priority < next priority != next priority && next priority What.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Conditions, logical expressions, and selection Introduction to control structures.
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.
C++ Control Flow Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th -22 nd Sept 2006.
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures Dale/Weems.
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.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
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.
1 Conditions, logical expressions, and selection Introduction to control structures.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
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.
Chapter 3 More Flow Of Control.
Lecture no 3 Control statements.
CPS120: Introduction to Computer Science Operations Lecture 9.
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.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
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.
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.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
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.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
ICT Introduction to Programming Chapter 4 – Control Structures I.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
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
Selection in C++ If statements. Control Structures Sequence Selection Repetition Module.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 5 Topics Data Type bool
CNG 140 C Programming (Lecture set 3)
Chapter 4: Making Decisions.
A mechanism for deciding whether an action should be taken
EGR 2261 Unit 4 Control Structures I: Selection
Control Structures – Selection
Conditionals & Boolean Expressions
Conditionals & Boolean Expressions
Chapter 4: Control Structures I (Selection)
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
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
Chap 7. Advanced Control Statements in Java
Presentation transcript:

1 Programming in C++ Dale/Weems/Headington Chapter 5 Conditions, Logical Expressions

2 Flow of Control l means the order in which program statements are executed WHAT ARE THE POSSIBILITIES...

3 FLOW OF CONTROL l is Sequential unless a “control structure” is used to change that l There are 2 general types of control structures: Selection (also called branching) Repetition (also called looping)

4 C++ control structures l Selection if if... else switch l Repetition for loop while loop do... while loop

5 CONTROL STRUCTURES Use logical expressions which may include: 6 Relational Operators >= == != 3 Logical Operators !&&||

6 In C++ l the value 0 represents false l ANY non-zero value represents true

7 Are used in expressions of form: ExpressionA Operator ExpressionB Temperature > Humidity B * B * A * C > 0.0 abs (Number ) == 35 Initial ! = ‘Q’ 6 Relational Operators

8 int x, y ; x = 4; y = 6; EXPRESSIONVALUE x < y1 (true) x + 2 < y0 (false) x != y1 (true) x + 3 >= y1 (true) y == x0 (false) y == x+21 (true) y = x + 37 (true)

9 OperatorMeaning Associativity ! NOTRight *, /, % Multiplication, Division, Modulus Left +, - Addition, SubtractionLeft < Less thanLeft <= Less than or equal toLeft > Greater thanLeft >= Greater than or equal toLeft == Is equal toLeft != Is not equal to Left && ANDLeft || OR Left = AssignmentRight

10 LOGICAL EXPRESSION MEANINGDESCRIPTION ! pNOT p! p is false if p is true ! p is true if p is false p && qp AND q p && q is true if both p and q are true. It is false otherwise. p || qp OR qp || q is true if either p or q or both are true. It is false otherwise.

11 int Age, Senior, Fever ; float Temperature ; Age = 20; Temperature = ; Senior = (Age >= 55) ; // Senior is 0 (false) Fever = (Temperature > 98.6) ; // Fever is 1 (true) EXPRESSIONVALUE Senior && Fever false Senior || Fever true ! Seniortrue ! Feverfalse

12 What is the value? int Age, Height; Age = 25; Height = 70; EXPRESSIONVALUE ! (Age < 10) ? ! (Height > 60) ?

13 “SHORT-CIRCUIT” EVALUATION l C++ uses short circuit evaluation of logical expressions l this means logical expressions are evaluated left to right and evaluation stops as soon as the final truth value can be determined

14 Short-Circuit Example int Age, Height; Age = 25; Height = 70; EXPRESSION (Age > 50) && (Height > 60) false Evaluation can stop now because result of && is only true when both sides are true. It is already determined that the entire expression will be false.

15 More Short-Circuiting int Age, Height; Age = 25; Height = 70; EXPRESSION (Height > 60) || (Age > 40) true Evaluation can stop now because result of || is true if one side is true. It is already determined that the entire expression will be true.

16 What happens? int Age, Weight ; Age = 25; Weight = 145; EXPRESSION (Weight = 20) true Must still be evaluated because truth value of entire expression is not yet known. Why? Result of && is only true if both sides are true.

17 What happens? int Age, Height; Age = 25; Height = 70; EXPRESSION ! (Height > 60) || (Age > 50) true false Does this part need to be evaluated?

18 Write an expression for each TaxRate is over 25% and Income is less than $20000 Temperature is less than or equal to 75 or Humidity is less than 70% Age is over 21 and Age is less than 60 Age is 21 or 22

19 SOME ANSWERS (TaxRate >.25) && (Income < 20000) (Temperature <= 75) || (Humidity <.70) (Age > 21) && (Age < 60) (Age == 21) || (Age == 22)

20 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.

21 SHORT-CIRCUIT BENEFITS l One boolean expression can be placed first to “guard” a potentially unsafe operation in a second boolean expression l Time is saved in evaluation of complex expressions using operators || and &&

22 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++.

23 WARNING about Expressions in C++ l “Boolean expression” means an expression whose value is true or false l An expression is any valid combination of operators and operands l Each expression has a value l This can lead to UNEXPECTED RESULTS l Construct your expressions CAREFULLY l Use of parentheses is encouraged l Otherwise, use precedence chart to determine order

24 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“ ;

25 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.

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