CSE 1301 J Lecture 2 Intro to Java Programming Richard Gesick.
CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software...
-
Upload
leonard-gaines -
Category
Documents
-
view
217 -
download
0
Transcript of CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software...
![Page 1: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/1.jpg)
CSE 1301
Lecture 8
Conditionals & Boolean Expressions
Figures from Lewis, “C# Software Solutions”, Addison Wesley
Richard Gesick
![Page 2: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/2.jpg)
CSE 1301
Topics
• Comparing– Floating point numbers– Objects– Strings
• Conditional Operator
![Page 3: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/3.jpg)
CSE 1301
Comparing Floating-Point Numbers• With IEEE 754 floating-point representation, minor
rounding errors can occur in calculations• We compute 11 * .1 two ways 1. Multiplying 11 * .1, the result is 1.1 2. Adding .1 11 times, the result is 1.0999999…• These values will not compare as equal using the
equality operator (==)• We get similar results when assigning the same value
to a float variable and to a double variable, then comparing the values.
![Page 4: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/4.jpg)
// Part 1: Compute 11 * .1 two ways
double d1 = .0; // add .1 to 0 eleven times
d1 += .1; // 1 d1 += .1; // 2 d1 += .1; // 3 d1 += .1; // 4 d1 += .1; // 5 d1 += .1; // 6 d1 += .1; // 7 d1 += .1; // 8 d1 += .1; // 9 d1 += .1; // 10 d1 += .1; // 11 double d2 = .1 * 11; // compute
11 * .1
C.O.Wln( "d1 = " + d1 ); C.O.Wln( "d2 = " + d2 ); if ( d1 == d2 )
C.O.Wln( "d1 and d2 are equal" );
else C.O.Wln( "d1 and d2 are not
equal" );// Part 2: Compare float and
double with same value float piF =
3.141592653589793f; double piD =
3.141592653589793;
C.O.Wln( "\npiF = " + piF ); C.O.Wln( "pid = " + piD ); if ( piF == piD ) C.O.Wln( "piF and piD are
equal" ); else C.O.Wln( "piF and piD are
not equal" );
• 1-4
![Page 5: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/5.jpg)
CSE 1301
Output
d1 = 1.0999999999999999d2 = 1.1d1 and d2 are not equal
piF = 3.1415927pid = 3.141592653589793piF and piD are not equal
![Page 6: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/6.jpg)
CSE 1301
Solution
• Choose a small threshold value -- how close should the values be to be considered equal?
• If the difference between the two values is less than the threshold value, then we will consider the two floating-point numbers to be equal.
• Hint: use the Math.Abs method to compute the difference.
![Page 7: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/7.jpg)
CSE 1301
// Part 1: Same as last example C.O.Wln( "d1 = " + d1 ); C.O.Wln( "d2 = " + d2 ); if ( Math.abs( d1 - d2 ) < .0001 ) C.O.Wln( "d1 and d2 are considered equal" ); else C.O.Wln( "d1 and d2 are not equal" );
// Part 2: Compare float and double with same value float piF = 3.141592653589793f; double piD = 3.141592653589793;
C.O.Wln( "\npiF = " + piF ); C.O.Wln( "piD = " + piD ); if ( Math.abs( piF - piD ) < .0001 ) C.O.Wln( "piF and piD are considered equal" ); else C.O.Wln( "piF and piD are not equal" );
![Page 8: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/8.jpg)
CSE 1301
Outputd1 = 1.0999999999999999d2 = 1.1d1 and d2 are considered equal
piF = 3.1415927pid = 3.141592653589793piF and piD are considered equal
![Page 9: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/9.jpg)
CSE 1301
Comparing Floats
• Problematic given rounding/precision• Pick a tolerance, and if the difference between the numbers is less
than this tolerance, consider the numbers equivalent
if ( Math.abs( d1 - d2 ) < THRESHOLD) C.WL("d1 and d2 are considered equal");else C.WL("d1 and d2 are not equal");
![Page 10: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/10.jpg)
CSE 1301
Comparing Characters
• What does it mean to “compare” 2 characters?
• Is ‘a’ < ‘b’?• Is ‘A’ > ‘z’?
![Page 11: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/11.jpg)
CSE 1301
Lexicographic Ordering
• Because all characters are “encoded” using the Unicode encoding scheme, Unicode values are compared.
• Lexicographic ordering is not strictly alphabetical when uppercase and lowercase characters are mixed
![Page 12: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/12.jpg)
CSE 1301
Comparing Objects• The equality operator ( == ) compares object
references. • Example:
– If d1 and d2 are two Date object references, then ( d1 == d2 ) evaluates to true only if d1 and d2 point to the same object, that is, the same memory location.
– The equality operator does not compare the data (month, day, and year) in those objects.
![Page 13: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/13.jpg)
CSE 1301
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-
Wesley
5-13
Comparing Object Data
With d1 and d2 Date object references: d1.equals(d2);Returns true if the month, day, and year of d1 equals the month,
day, and year of d2.
Return type
Method name and argument list
boolean equals( Object obj )
returns true if the data of the object obj is equal to the data in the object used to call the method
![Page 14: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/14.jpg)
CSE 1301
// instantiate two Date objects with identical data Date d1 = new Date( 4, 10, 2006 ); Date d2 = new Date( 4, 10, 2006 );
// assign object reference d1 to d3 Date d3 = d1; // d3 now points to d1
// instantiate another object with different data Date d4 = new Date( 12, 1, 2006 );
Do not use the equality operators (==, !=) to compare object data; instead, use the equals method.
![Page 15: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/15.jpg)
CSE 1301
Comparing Date Objects
![Page 16: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/16.jpg)
CSE 1301
// compare references using equality operator if ( d1 == d2 ) C.WL( "d1 and d2 are equal\n" ); else C.WL( "d1 and d2 are not equal\n" );
if ( d1 == d3 ) C.WL( "d1 and d3 are equal\n" ); else C.WL( "d1 and d3 are not equal\n" );
// compare object data using the equals method if ( d1.equals( d2 ) ) C.WL( "d1 data and d2 data are equal\n" ); else C.WL( "d1 data and d2 data are not equal\n" );
if ( d1.equals( d4 ) ) C.WL( "d1 data and d4 data are equal" ); else C.WL( "d1 data and d4 data are not equal" );
Bad
Good
![Page 17: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/17.jpg)
CSE 1301
The Conditional Operator (?:)• The conditional operator ( ?: ) contributes one of two
values to an expression based on the value of the condition.
• Some uses are – handling invalid input – outputting similar messages.
• Syntax: ( condition ? trueExp : falseExp ) If condition is true, trueExp is used in the expression If condition is false, falseExp is used in the expression
![Page 18: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/18.jpg)
CSE 1301
Equivalent Code• The following statement stores the absolute value of
the integer a into the integer absValue. int absValue = ( a > 0 ? a : -a );• The equivalent statements using if/else are: int absValue; if ( a > 0 ) absValue = a; else absValue = -a;
![Page 19: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/19.jpg)
CSE 1301
The Conditional Operator• Another example:
C.O.Wln ("Your change is " + count + ((count == 1) ? "Dime" : "Dimes"));
• If count equals 1, then "Dime" is printed
• If count is anything other than 1, then "Dimes" is printed
![Page 20: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/20.jpg)
CSE 1301
Nested if Statements• if statements can be written as part of the true or
false block of another if statement.• Typically, you nest if statements when more
information is required beyond the results of the first if condition
• The compiler matches any else clause with the most previous if statement that doesn't already have an else clause.
• You can use curly braces to force a desired if/else pairing.
![Page 21: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/21.jpg)
CSE 1301
Exampleif ( x == 2 ) if ( y == x ) C.O.Wln( "x and y equal 2" ); else
C.O.Wln( "x equals 2 but y does not" );
• The else clause is paired with the second if , that is: if ( y == x )
![Page 22: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/22.jpg)
CSE 1301
Another Exampleif ( x == 2 ){ if ( y == x ) C.O.Wln( "x and y equal 2" );} else C.O.Wln( "x does not equal 2" );
• With curly braces added, the else clause is paired with the first if , that is: if ( x == 2 )
![Page 23: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/23.jpg)
CSE 1301
The "Dangling else"• A dangling else is an else clause that cannot be
paired with an if conditionif ( x == 2 ) if ( y == x ) C.O.Wln( "x and y equal 2" ); else // paired with ( y == x ) C.O.Wln( "y does not equal 2" );else // paired with ( x == 2 ) C.O.Wln( "x does not equal 2" );else // no matching if! C.O.Wln( "x and y are not equal" );
• Generates the compiler error: 'else' without 'if'
![Page 24: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/24.jpg)
CSE 1301
In the absence of braces, an else is always paired with the closest preceding if that doesn’t already have an else paired with it.
![Page 25: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/25.jpg)
CSE 1301
Bad Example has output: FAIL
float average;
average = 100.0;
if ( average >= 60.0 )if ( average < 70.0 ) C.O.Wln(“Marginal PASS”);
elseC.O.Wln(“FAIL”);
WHY? The compiler ignores indentation and pairs the else with the second if.
average
100.0
![Page 26: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/26.jpg)
CSE 1301
To correct the problem, use braces
float average;
average = 100.0;
if ( average >= 60.0 ){
if ( average < 70.0 ) C.O.Wln(“Marginal PASS”);
}else
C.O.Wln(“FAIL”);
average
100.0
![Page 27: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/27.jpg)
CSE 1301
The switch Statement
• Sometimes the switch statement can be used instead of an if/else/if statement for selection.
• Requirements:– we must be comparing the value of a character
(char) or integer (byte, short, or int) expression to constants of the same types
![Page 28: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/28.jpg)
CSE 1301
Syntax of switch
switch ( char or integer expression ) { case constant1: // statement(s);
break; case constant2:
// statement(s); break;
… default: // optional
statement(s); …}
![Page 29: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/29.jpg)
CSE 1301
Operation of switch• The expression is evaluated, then its value is
compared to the case constants in order. • When a match is found, the statements under
that case constant are executed in sequence until either a break statement or the end of the switch block is reached.
• Once a match is found, if other case constants are encountered before a break statement, then the statements for these case constants are also executed.
![Page 30: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/30.jpg)
CSE 1301
Some Finer Points of switch• The break statements are required. Their job is to
terminate execution of the switch statement. • The default label and its statements are optional.
They are executed when the value of the expression does not match any of the case constants.
• The statements under the case constant are also optional, so multiple case constants can be written in sequence if identical operations will be performed for those values.
• You cannot perform relational checks with a switch statement
![Page 31: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/31.jpg)
CSE 1301
Example: a Simple Calculator• Prompt user for two doubles (num1, num2) and a char
(operation), which can be 'a' for addition or 's' for subtractionswitch ( operation ){case 'a':
result = num1 + num2;break;
case 's':result = num1 - num2;break;
}
![Page 32: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/32.jpg)
CSE 1301
A Case-Insensitive Calculatorswitch ( operation ){case 'a':
case 'A':result = num1 + num2;break;
case 's': case 'S':
result = num1 - num2;break;
}
![Page 33: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/33.jpg)
CSE 1301
float weightInPounds = 165.8 f;char weightUnit ;
. . . // user enters letter for desired weightUnitswitch ( weightUnit ){
case ‘P’ :case ‘p’ :
C.O.Wln(weightInPounds + “ pounds “ ) ;break ;
case ‘O’ :case ‘o’ :
C.O.Wln(16.0 * weightInPounds + “ ounces “ ) ;break ;
case ‘K’ :case ‘k’ :
C.O.Wln(weightInPounds / 2.2 + “ kilos “ ) ;break ;
default :C.O.Wln(“That unit is not handled! “ ) ;break ;
}
![Page 34: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/34.jpg)
CSE 1301
Use Switch for Menusswitch (edit_op) {
case ‘D’: case ‘d’: // Delete a substring
doc.do_delete (text_string); break;
case ‘F’: case ’f’: // Find a substring
doc.do_find (text_string); break;case ‘R’:
case ‘r’: // Replace a substringdoc.do_replace (text_string); break;
default: C.O.Wln(“Invalid edit code entered.”);
} // end switch;
![Page 35: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/35.jpg)
CSE 1301
Write an expression for eachWrite a logical assignment statement for each of the following:• assign a value of true to between if n is in the range of -k to
+k, inclusive; otherwise assign a value of false.
• assign a value of true to uppercase is ch is an uppercase letter; otherwise, assign a value of false.
• assign a value of true to divisor if m is a divisor of n; otherwise assign a value of false.
![Page 36: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/36.jpg)
CSE 1301
What’s the output? and Why?int age =20;
if ( age = 16 ){
C.O.Wln(“Did you get driver’s license?”) ;}
![Page 37: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/37.jpg)
CSE 1301
What’s the output? and Why?
int age =30;
if ( age < 18 )
C.O.Wln(“Do you drive?”); C.O.Wln(“Too young to vote”);
![Page 38: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/38.jpg)
CSE 1301
What’s the output? and Why?boolean code = false;
if ( ! code )
C.O.Wln(“Yesterday”);else
C.O.Wln(“Tomorrow”);
![Page 39: CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.](https://reader035.fdocuments.net/reader035/viewer/2022062721/56649f1f5503460f94c37c6e/html5/thumbnails/39.jpg)
CSE 1301
What’s the output? and Why?
if (x > y) x = x + 10.0;C.O.Wln(“x bigger” );elseC.O.Wln(“x smaller” );C.O.Wln(“y is “ + y );