COM S 207 IF Statement Instructor: Ying Cai Department of Computer Science Iowa State University
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
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
Example if (originalPrice > 100) { discountPrice = originalPrice – 20; } else { discountPrice = originalPrice – 10; } originalPrice discountPrice good to use braces
How about swapping the two statements if (originalPrice < 100) { discountPrice = originalPrice – 10; } else { discountPrice = originalPrice – 20; } originalPrice discountPrice different when originalPrice is 100
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
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
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)
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 “==“
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
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
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
Read inputs: (married, income) Start single? income <= income <= rate = 15%rate = 28%rate = 31% income <= income <= rate = 15%rate = 28% rate = 31% Yes No Yes No 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 HorseGoatMonkeyRoosterDogPigMouseOXTigerRabbitDragonSnake 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; }