Caution Adding a semicolon at the end of an if clause is a common mistake if (radius >=0) Wrong area radius radius*Pl System. out. printIn( The area for the circle of radius radius +is"+ area) ◆ logic error This error often occurs when you use the next-line block style Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 11 Caution Adding a semicolon at the end of an if clause is a common mistake. if (radius >= 0); { area = radius*radius*PI; System.out.println( "The area for the circle of radius " + radius + " is " + area); } ◆logic error. ◆This error often occurs when you use the next-line block style. Wrong
The if.else Statement if(booleanExpression < ne Can't use 0,1 statement(s)-for-the-true-casei What if there is no else statement the difference else statement(s)-for-the-false-casei true false Boolean Expression Statement(s)for the true Statement(s)for the false case
Liang,Introduction to Java Programming,revised by Dai-kaiyu 12 The if...else Statement if (booleanExpression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; } Boolean Expression true false Statement(s) for the true case Statement(s) for the false case What if there is no else statement, the difference Can’t use 0,1
if.else Example if (radius >=0)( area radius radius 3.14159 System. out. println( The area for the +circle of radius t radius t is+ area)i e⊥se System. out.println("Negative input")i Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 13 if...else Example if (radius >= 0) { area = radius * radius * 3.14159; System.out.println("The area for the “ + “circle of radius " + radius + " is " + area); } else { System.out.println("Negative input"); }
Multiple Alternative if Statements (eD if(score >=90.0) f (score >=90.0) grade grade A else else if (score >=80.0) if (score >=80.0) Equivalent grade B grade B else if (score >= 70.0) else grade C if (score >=70.0) else if (score >=60.0) grade grade D else else if (score >=60.0) grad F grade se grade =Fli better Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 14 Multiple Alternative if Statements if (score >= 90.0) grade = 'A'; else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F'; Equivalent if (score >= 90.0) grade = 'A'; else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F'; better
Trace if-else statement Suppose score is 70.0 The condition is false if(score >=90.0 grade A else if (score >=80.0) grade=B else if(score >=70.0) grade C else if(score >=60.0) grade =D e ade =F Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 15 Trace if-else statement if (score >= 90.0) grade = 'A'; else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F'; Suppose score is 70.0 The condition is false