Copyright © 2004-2014 Curt Hill The C++ IF Statement More important details More fun Part 3.

Slides:



Advertisements
Similar presentations
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Advertisements

Copyright © by Curt Hill Expressions and Assignment Statements Part 2.
ISBN Chapter 7 Expressions and Assignment Statements.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Logical Operators and Conditional statements
ISBN Lecture 07 Expressions and Assignment Statements.
Expressions & Assignments One of the original intentions of computer programs was the evaluation of mathematical expressions –languages would inherit much.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
ISBN Chapter 7 Expressions and Assignment Statements.
ISBN Chapter 7 Expressions and Assignment Statements.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Copyright © – Curt Hill Types What they do.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Copyright Curt Hill Casts What are they? Why do we need them?
Copyright Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Rational Expressions relational operators logical operators order of precedence.
The for Statement A most versatile loop
Expressions and Assignment Statements
Expressions and Assignment Statements
Control Structures I Chapter 3
The Ohio State University
Basic concepts of C++ Presented by Prof. Satyajit De
7.2 Arithmetic Expressions
Data Types and Expressions
Chapter 7: Expressions and Assignment Statements
Operators and Expressions
Expressions and Assignment Statements
Side Effect Operators Changing the value of variables
Selections Java.
Computing Fundamentals
More important details More fun Part 3
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 7: Expressions and Assignment Statements
Variables and Primative Types
Boolean Expressions and if-else Statements
What are they? Why do we need them?
Expressions and Assignment Statements
Control Structures – Selection
Expressions and Assignment Statements
Expressions and Assignment Statements
College of Computer Science and Engineering
Selection Control Structure
What are they? Why do we need them?
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
The Java switch Statement
Boolean Expressions to Make Comparisons
Chapter 7 Expressions and Assignment Statements.
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
OPERATORS AND EXPRESSIONS IN C++
CprE 185: Intro to Problem Solving (using C)
OPERATORS in C Programming
Operator King Saud University
PRESENTED BY ADNAN M. UZAIR NOMAN
The IF Revisited A few more things Copyright © Curt Hill.
Expressions and Assignment Statements
Controlling Program Flow
The IF Revisited A few more things Copyright © Curt Hill.
OPERATORS in C Programming
Presentation transcript:

Copyright © Curt Hill The C++ IF Statement More important details More fun Part 3

Copyright © Curt Hill Boolean Variables Recall that bool is a reserved word It indicates a type that may only have two values: true, false –true and false are constant values Use whenever only two values are possible: bool ismanager, isfemale, ismember; Variables of type boolean often start with the word is, because it indicates type

Copyright © Curt Hill Comparisons Again Comparison operators are not boolean operators Recall that an integer operator like % takes two integers and produces an integer A comparison may compare two integers and produce a boolean A boolean operator does a logical operation on two booleans

Copyright © Curt Hill Boolean Operators Boolean operators are the AND, OR and NOT C operators: – && || ! The single & and | are bit string operators not booleans AND, OR, and NOT have the same meaning as is commonly used in English AND/OR have lower precedence than comparisons

Copyright © Curt Hill C Operator Truth Table PQP && QP || Q!P true false truefalse truefalse truefalsetrue false true

Copyright © Curt Hill Boolean Comparisons A sure sign of a novice is to compare a boolean with a truth value: bool b; if(b == true) // or if(b == false) // or if(b != true) A boolean is already suitable for comparison Use one of these two: if(b) // or if(!b)

Copyright © Curt Hill Comparing Real Values The whole issue of roundoff makes comparisons of real numbers problematic Consider the following: double d = 1.0, e = 10.0; double f = d/e; … if(f*10 == d) … This condition can never be true!

Copyright © Curt Hill Why not? Our normal understanding is that they should be equal and the comparison true We usually think algebraically, that is with infinite precision The decimal 0.1 is a repeating fraction in binary –It cannot be represented in a finite number of digits The comparison then becomes something like this: if(1 == )

However This kind of comparison is chancy but aided by the ways a Pentium does arithmetic It always uses one more digit of arithmetic The default mode is to then round that into the right size That rounding bails us out in certain cases Copyright © Curt Hill

So what do we do? We never compare floats or doubles for equality or inequality! –In general, equality is never true when we want it to be and inequality is always true We have to be satisfied that the two values are close, even if they are not quite equal

Copyright © Curt Hill Real Comparisons Suppose that we want to compare two doubles a and b for equality Round-off error makes them close but not equal, so use: if(abs(a-b) < ) This is true only if the difference between the two is less than 1E-5 The is the fuzz factor –Needs to be larger than the expected round-off error, but smaller than actual differerences

Alternative Names The C standard allows for some alternatives to some of the symbols Allows a symbol to have a reserved word: –not for ! –not_eq for != –and for && –or for || –Among others Copyright © Curt Hill

Precedence Again (postfix) (prefix) + - (unary) casts ! * / % + - >= <= == != && || = *= += -= /= %=

Copyright © Curt Hill Booleans Revisited For about 20 years C did not have a type bool It had the if, it had comparisons and it even had boolean operators So what did it do? It had a convention

Copyright © Curt Hill The C Boolean Convention Any type could function as a boolean If the value was zero it meant false If the value was not zero it meant true Thus a character, integer, float could pass for a bool C++ has maintained this –Java did not

Copyright © Curt Hill Examples Given int a = 2, b = 0, c = -5; if(a) // true … if(b) // false … if(c) // true … All these work and do not generate syntax errors

Copyright © Curt Hill The tricky one The reason this works: if (a = b) The value assigned to a is returned as the result This will be interpreted as false if zero and true otherwise Allows us to both assign and drive an if at the same time However, it is usually a mistake, a left out =

Compilers The: if(a = b)… is legal and actually good C/C++ It allows a common mistake Borland/Codegear/Embarcadero compilers give a warning –Like all warnings it may be ignored Microsoft Visual Studio does not Copyright © Curt Hill

What do comparison operators do Whenever a comparison is done a 0/1 is always produced This is usually cast as a bool but not always int a = b>5; will produce 0 or 1 in a –But no error

Copyright © Curt Hill The Real Tricky One Suppose that int a = 5, b= 4, c=9; The comparison: if(a<b<c) will be true Why? 5 0 // false 0 1 // true Always use (a<b && b<c)

Copyright © Curt Hill Evaluation of the condition There are states in the truth table where we do not care about the second operand –TRUE OR TRUE is the same as TRUE OR FALSE –The second operand does not matter –TRUE OR ? is TRUE Similarly FALSE AND ? is FALSE

Copyright © Curt Hill Short Circuit Evaluation The compiler knows this When ever it knows what the answer will be it stops evaluating Example: if(a!=0 && b/a>12) should always work When a == 0 then the compiler knows that FALSE AND ? is FALSE so does not need to evaluate the b/a>12 –Avoids the zero division exception

A Problem Suppose I have a truly ugly condition: if(a+2>5 && b-3<c*2) I want to do nothing if this is true and several things in a compound statement if it is false Copyright © Curt Hill

Reversing Conditions Sometimes we have difficulty with these types of conditions A complicated condition may be difficult to reverse There are three solutions: –Negation –Do nothing then –DeMorgan’s Law Copyright © Curt Hill

Negation One possibility is negating the entire expression Introduce one more pair of parentheses and insert the negation operator ! Try this: if(!(a+2>5 && b-3<c*2)) {…} Copyright © Curt Hill

Empty statement An empty statement is just a semicolon It says “do nothing” Try this: if(a+2>5 && b-3<c*2) ; // The empty stmt else {…}

DeMorgan’s Laws Any And may be reversed by negating the conditions and reversing the And to an OR –!(a>b && c d Any Or may be reversed by negating the conditions and reversing the Or to an And !(a==b || c>=d) becomes a!=b && c<d Copyright © Curt Hill

DeMorgan’s Solution Applying this we can solve the problem of the previous if: if(a+2>5 && b-3<c*2) Try this instead: if(a+2 =c*2) {…} Copyright © Curt Hill

Readability The compiler ignores white space! People do not ignore white space! Use indentation to improve the readability of your programs Indent the statements controlled by an If or any other flow of control statement The change in left margin is a powerful visual clue to the reader

Copyright © Curt Hill Example of Readability Use: if(a>b){ x = a*b; y += 5; } cin >> s; Do not: if(a>b){ x = a*b; y += 5;} cin >> s; Use: if(a==c){ x = a*b; y += 5;... else { y *= 2;... Do not: if(a==c){ x = a*b; y += 5;... else { y *= 2;...