com s 207 if statement instructor: ying cai department of computer science iowa state university...
TRANSCRIPT
COM S 207IF Statement
Instructor: Ying Cai
Department of Computer ScienceIowa State [email protected]
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
95100105
discountPrice
859085
good to use braces
How about swapping the two statements
if (originalPrice < 100){ discountPrice = originalPrice – 10;}else{ discountPrice = originalPrice – 20;}
originalPrice
95100105
discountPrice
8510085
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)
relationaloperators
> greater than< less than>= greater than or equal to<= less than or equal to== equal to!= not equal to
Expression Value
3 <= 4 true3 =< 4 ERROR3 > 4 false4 < 4 false4 <= 4 true3 == 5-2 true3 = 6/2 ERROR“10 > 5 ERROR
• 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 falseif (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 ScaleValue Effect8 most structures fall7 many buildings destroyed6 many buildings damaged4.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 ScaleValue Effect8 most structures fall7 many buildings destroyed6 many buildings damaged4.5 damaged to poorly constructed buildings
Example: Federal Tax Rate Schedule
Single married
Taxable income rate Taxable income rate
<=$21,450 15% <=$35,800 15%
Over $21,450 but <= $51,900
28% Over $35,800 but <= $86,500 28%
Over $51,900 31% Over $86,500 31%
(1)Flow chart
(2)Test cases
(3)Code
your code
input
1. amount of tax due
1. marriage_status2. income
output
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
Yes
Yes
Yes
No
No
No
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
Horse Goat Monkey Rooster Dog Pig Mouse OX Tiger Rabbit Dragon Snake
2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001
1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977
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;}