Docsity

Lecture Slides on Comparing, Conditional Operator | CSE 1301, Study notes of Computer Science

Toggle sidebar
bg1
CSE 1301
Lecture 5B
More Conditionals
Figures from Lewis, ā€œC# Software Solutionsā€, Addison Wesley
Briana B. Morrison
CSE 1301 5B-2
Topics
• Comparing
– Floating point numbers
– Objects
– Strings
• Conditional Operator
CSE 1301 5B-3
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.
CSE 1301 5B-4
/
/ 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" );
CSE 1301 5B-5
Output
d1 = 1.0999999999999999
d2 = 1.1
d1 and d2 are not equal
piF = 3.1415927
pid = 3.141592653589793
piF and piD are not equal
CSE 1301 5B-6
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.
pf3
pf4
pf5
pf8

Partial preview of the text

Download Lecture Slides on Comparing, Conditional Operator | CSE 1301 and more Study notes Computer Science in PDF only on Docsity!

CSE 1301

Lecture 5B

More Conditionals

Figures from Lewis, ā€œC# Software Solutionsā€, Addison Wesley

Briana B. Morrison

CSE 1301 (^) 5B-

Topics

  • Comparing
    • Floating point numbers
    • Objects
    • Strings

• Conditional Operator

CSE 1301 (^) 5B-

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.

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.

CSE 1301 (^) 5B-

// 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 *. 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" );

Output

d1 = 1.

d2 = 1.

d1 and d2 are not equal

piF = 3.

pid = 3.

piF and piD are not equal

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.

CSE 1301 (^) 5B-

// 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" );

CSE 1301 (^) 5B-

Output

d1 = 1.

d2 = 1.

d1 and d2 are considered equal

piF = 3.

pid = 3.

piF and piD are considered equal

CSE 1301 (^) 5B-

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");

CSE 1301 (^) 5B-

Comparing Characters

• What does it mean to ā€œcompareā€ 2

characters?

• Is ā€˜a’ < ā€˜b’?

• Is ā€˜A’ > ā€˜z’?

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

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.

CSE 1301 (^) 5B-

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

CSE 1301 (^) 5B-

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.

CSE 1301 (^) 5B-

Example

if ( 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 )

CSE 1301 (^) 5B-

Another Example

if ( 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 )

The "Dangling else "

• A dangling else is an else clause that cannot

be paired with an if condition

if ( 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'

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.

CSE 1301 (^) 5B-

Bad Example has output: FAIL

float average;

average = 100.0;

if ( average >= 60.0 ) if ( average < 70.0 ) C.O.Wln(ā€œMarginal PASSā€); else C.O.Wln(ā€œFAILā€);

WHY? The compiler ignores indentation

and pairs the else with the second if.

average

CSE 1301 (^) 5B-

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

CSE 1301 (^) 5B-

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

CSE 1301 (^) 5B-

Syntax of switch

switch ( char or integer expression ) { case constant1: // statement(s); break; // optional case constant2: // statement(s); break; // optional … default: // optional statement(s); … }

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.

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

CSE 1301 (^) 5B-

Write an expression for each

  • age is from 18 to 21 inclusive.
  • water is less than 1.5 and is also greater than 0.1.
  • year is divisible by 4
  • speed is not greater than 55.

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

CSE 1301 (^) 5B-

Write If-Then or If-Then-Else for each

If taxCode is ā€˜T’, increase price by adding

taxRate times price to it.

If code has value 1, calculate and display

taxDue as the product of income and

taxrate.

If A is strictly between 0 and 5, set B equal to

1/A, otherwise set B equal to A.

CSE 1301 (^) 5B-

Some Answers

if (taxCode == ā€˜T’)

price = price + taxRate * price;

if ( code == 1) { taxDue = income * taxRate; C.O.Wln(taxDue); }

CSE 1301 (^) 5B-

Remaining Answer

if ( ( A > 0 ) && (A < 5) )

B = 1/A;

else

B = A;

What’s the output? and Why?

int age;

age = 20;

if ( age = 16 )

C.O.Wln(ā€œDid you get driver’s license?ā€) ;

What’s the output? and Why?

int age;

age = 30;

if ( age < 18 )

C.O.Wln(ā€œDo you drive?ā€);

C.O.Wln(ā€œToo young to voteā€);

What’s the output? and Why?

boolean code;

code = false;

if (! code )

C.O.Wln(ā€œYesterdayā€);

else

C.O.Wln(ā€œTomorrowā€);

What’s the output? and Why?

if (x > y)

x = x + 10.0;

C.O.Wln(ā€œx biggerā€ );

else

C.O.Wln(ā€œx smallerā€ );

C.O.Wln(ā€œy is ā€œ + y );