Presentation is loading. Please wait.

Presentation is loading. Please wait.

COM S 207 IF Statement Instructor: Ying Cai Department of Computer Science Iowa State University

Similar presentations


Presentation on theme: "COM S 207 IF Statement Instructor: Ying Cai Department of Computer Science Iowa State University"— Presentation transcript:

1 COM S 207 IF Statement Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu

2 Used to Implement decisions Two key elements condition body amount <= balance balance = balance - amount true false condition body if (amount <= balance) { balance = balance – amount; } if-then

3 Used to Implement decisions Two key elements condition body amount <= balance balance = balance - amount true false condition body balance = balance – OVERDRAFT_PENALTY body if (amount <= balance) { balance = balance – amount; } else { balance = balance – OVERDRAFT_PENALTY; } if-then-else

4 Example if (originalPrice > 100) { discountPrice = originalPrice – 20; } else { discountPrice = originalPrice – 10; } originalPrice 95 100 105 discountPrice 85 90 85 good to use braces

5 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

6 Conditional Operator condition ? value1 : value2 actualFloor = floor > 13 ? floor-1 : floor; if (floor > 13) { actualFloor = floor – 1; } else { actualFloor = floor; } you will find this in many java programs, but not recommended for now

7 Relational Operators: Comparing Numbers if (floor operator 13) relational operators >greater than <less than >=greater than or equal to <=less than or equal to ==equal to !=not equal to ExpressionValue 3 <= 4true 3 =< 4ERROR 3 > 4false 4 < 4false 4 <= 4true 3 == 5-2true 3 = 6/2ERROR “10 > 5ERROR use == for equality cannot compare a string to a number

8 Comparing Floating-point Numbers 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); } Floating-point numbers have only a limited precision, and calculations can introduce round-off errors 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)

9 Comparing Strings 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 Use method “compareTo” instead of “==“

10 Nested Branches if (condition1) { … } else if (condition 2) { … } else if (condition 3) { … } else { … } Richter Scale ValueEffect 8 most structures fall 7 many buildings destroyed 6 many buildings damaged 4.5 damaged to poorly constructed buildings

11 Nested Branches 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”); } Richter Scale ValueEffect 8 most structures fall 7 many buildings destroyed 6 many buildings damaged 4.5 damaged to poorly constructed buildings

12 Example: Federal Tax Rate Schedule Singlemarried Taxable incomerateTaxable incomerate <=$21,45015%<=$35,80015% Over $21,450 but <= $51,90028%Over $35,800 but <= $86,50028% Over $51,90031%Over $86,50031% (1)Flow chart (2)Test cases (3)Code your code input 1. amount of tax due 1.marriage_status 2.income output

13 Read inputs: (married, income) Start single? income <= 21450 income <= 51900 rate = 15%rate = 28%rate = 31% income <= 35800 income <= 86500 rate = 15%rate = 28% rate = 31% Yes No Yes No Compute due tax: rate * income End

14 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).

15 One More Example HorseGoatMonkeyRoosterDogPigMouseOXTigerRabbitDragonSnake 200220032004200520062007200820092010201120122013 19901991199219931994 1995199619971998199920002001 197819791980198119821983198419851986198719881989 196619671968196919701971197219731974197519761977 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 ”.

16 Switch Statement switch (variable) { case value1: body1 break; case value2: body2; break; case value3: body3; break; ::: default: body; break; }


Download ppt "COM S 207 IF Statement Instructor: Ying Cai Department of Computer Science Iowa State University"

Similar presentations


Ads by Google