Ex7ElseIf

This preview shows pages 1–2. Sign up to view the full content.

Example 7: If-else, if-elseif, This shows only code snippets, not an entire program First problem: If grade is at least 60, print "passed." Otherwise, print "failed." The grade is recorded in the variable grade. This is how you know to do it already: if (grade >= 60) System.out.println("Passed"); if (grade < 60) System.out.println("Failed"); This is our new way to do it: if (grade >= 60) System.out.println("Passed"); // fill in the correct code here System.out.println("Failed"); Okay, how about something harder? Print out the grade, from A to F. This is how you know to do it: if (grade >= 90) System.out.println("A");

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: if ((grade < 90) && (grade >= 80)) System.out.println("B"); if ((grade < 80) && (grade >= 70)) System.out.println("C"); if ((grade < 70) && (grade >= 60)) System.out.println("D"); if (grade < 60) System.out.println("F"); This is how you should do it (fill in the correct code): if (grade >= 90) System.out.println("A"); System.out.println("B"); System.out.println("C"); System.out.println("D"); System.out.println("F"); Dangerous use of if – else if (grade >= 60) if (grade < 70) System.out.println("D"); else System.out.println("F"); How will this work?...
View Full Document