160 likes | 172 Views
COM S 207 IF Statement Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu. Used to Implement decisions. condition. Two key elements condition body. amount <= balance. false. body. true. balance = balance - amount. if-then.
E N D
COM S 207 IF Statement Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu
Used to Implement decisions condition • Two key elements • condition • body amount <= balance false body true balance = balance - amount if-then if (amount <= balance) { balance = balance – amount; }
Used to Implement decisions condition • Two key elements • condition • body amount <= balance false true body body balance = balance - amount balance = balance – OVERDRAFT_PENALTY if (amount <= balance) { balance = balance – amount; } else { balance = balance – OVERDRAFT_PENALTY; } if-then-else
Example if (originalPrice > 100) { discountPrice = originalPrice – 20; } else { discountPrice = originalPrice – 10; } good to use braces originalPrice 95 100 105 discountPrice 85 90 85
How about swapping the two statements if (originalPrice < 100) { discountPrice = originalPrice – 10; } else { discountPrice = originalPrice – 20; } originalPrice 95 100 105 discountPrice 85 100 85 different when originalPrice is 100
Conditional Operator • condition ? value1 : value2 actualFloor = floor > 13 ? floor-1 : floor; you will find this in many java programs, but not recommended for now if (floor > 13) { actualFloor = floor – 1; } else { actualFloor = floor; }
Relational Operators: Comparing Numbers if (floor operator 13) > greater than< less than >= greater than or equal to <= less than or equal to == equal to != not equal to relational operators ExpressionValue 3 <= 4 true 3 =< 4 ERROR 3 > 4 false 4 < 4 false 4 <= 4 true 3 == 5-2 true 3 = 6/2 ERROR “10 > 5 ERROR • use == for equality • cannot compare a string to a number
Comparing Floating-point Numbers • Floating-point numbers have only a limited precision, and calculations can introduce round-off errors double r = Math.sqrt(2.0); if (r * r == 2.0) { System.out.println(“Math.sqrt(2.0) squared is 2.0”); } else { System.out.println(“Math.sqrt(2.0) squared is” + r * r); } In most cases, it does not make sense to compare to two reals exactly. Instead, we should test whether they are close enough: |x-y| < epsilon (where epsilon can be set to very small, say 10e-14)
Comparing Strings • Use method “compareTo” instead of “==“ String s1 = “Robert”; String s2 = s1.substring(0, 3); // s2 = “Rob” if (s2 == “Rob”) // test is false if (s2.compareTo(“Rob”) == 0) // test is true
Nested Branches if (condition1) { … } else if (condition 2) { … } else if (condition 3) { … } else { … } • Richter Scale • Value Effect • most structures fall • 7 many buildings destroyed • many buildings damaged • 4.5 damaged to poorly constructed buildings
Nested Branches • Richter Scale • Value Effect • most structures fall • 7 many buildings destroyed • many buildings damaged • 4.5 damaged to poorly constructed buildings if (richter >= 8.0) { System.out.println(“Most structures fall”); } else if (richter >= 7.0) { System.out.println(“Many buildings destroyed”); } else if (richter >= 6.0) { System.out.println(“many buildings damaged, some collapse”); } else if (richter >= 4.5) { System.out.println(“Damage to poorly constructed buildings”); }
Example: Federal Tax Rate Schedule • marriage_status • income input • Flow chart • Test cases • Code your code output 1. amount of tax due
Start Read inputs: (married, income) Yes No single? income <= 35800 income <= 21450 Yes Yes No No income <= 86500 income <= 51900 Yes Yes No No rate = 31% rate = 31% rate = 28% rate = 15% rate = 15% rate = 28% Compute due tax: rate * income End
Another Example Write (1) test cases, (2) flowchart, and (3) code to prompt the user for three sides (of type INT) of a triangle. Then check if it is an equilateral triangle (i.e., all three sides are equal), else check if it is an isosceles triangle (i.e., two sides are equal). Print out if it is an equilateral, isosceles, or scalene triangle (i.e., no sides are equal).
One More Example The celebration of the Chinese Lunar year goes back centuries. There are 12 different creatures represented in the Chinese calendar. A different animal is commemorated each year. After 12 years the animals are repeated. If you were born in between 1966 and 2013, you can use the following table to find out your “year animal”. For examples, if you were born in 1990, your year animal is “Horse”; if you were born in 1991, your year animal is “Rabbit”.
Switch Statement switch (variable) { case value1: body1 break; case value2: body2; break; case value3: body3; break; ::: default: body; break; }