Ex7ElseIf

Ex7ElseIf - if ((grade < 90) && (grade >= 80))

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

View Full Document Right Arrow Icon
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");
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
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

This note was uploaded on 12/27/2011 for the course CMPSC 5 taught by Professor Franklin during the Fall '09 term at UCSB.

Page1 / 2

Ex7ElseIf - if ((grade < 90) && (grade >= 80))

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online