Presentation is loading. Please wait.

Presentation is loading. Please wait.

More important details More fun Part 3

Similar presentations


Presentation on theme: "More important details More fun Part 3"— Presentation transcript:

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

2 Introduction We have several topics we need to master:
Boolean variables and operators Danger of comparing real numbers Alternative symbols Boolean convention Reversing a Boolean expression Copyright © Curt Hill

3 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

4 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

5 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

6 C Operator Truth Table P Q P && Q P || Q !P true false
Copyright © Curt Hill

7 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

8 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) … On many machines this condition can never be true! Copyright © Curt Hill

9 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 == ) Where there are digits Copyright © Curt Hill

10 You Get Lucky This kind of comparison is chancy but aided by the ways a Pentium and its successors do arithmetic These machines always use 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 If roundoff errors get too large even this will not help Copyright © Curt Hill

11 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

12 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 differences Copyright © Curt Hill

13 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

14 Precedence Again ++ -- (postfix) ++ -- (prefix) + - (unary) casts !
* / % + - < > >= <= == != && || = *= += -= /= %= Copyright © Curt Hill

15 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

16 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

17 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

18 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 = Copyright © Curt Hill

19 Compilers The: if(a = b)… is legal and actually good C/C++
It allows a common mistake Some compilers give a warning Like all warnings it may be ignored Others do not We should check ours Copyright © Curt Hill

20 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

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

22 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

23 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 Copyright © Curt Hill

24 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

25 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

26 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

27 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 {…} Copyright © Curt Hill

28 DeMorgan’s Laws Any And may be reversed by negating the conditions and reversing the And to an OR !(a>b && c<=d) becomes 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

29 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<=5 || b-3>=c*2) {…} Copyright © Curt Hill

30 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

31 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; . . . Copyright © Curt Hill


Download ppt "More important details More fun Part 3"

Similar presentations


Ads by Google