1 / 14

Comp 110 Conditionals

Comp 110 Conditionals. Instructor: Prasun Dewan. Prerequisite. Types Math . Conditionals. printPassFailStatus (95). Passed. printPassFailStatus (25). Failed. public static void printPassFailStatus ( int score) { if (score < PASS_CUTOFF) System.out.println ("FAILED"); else

ted
Download Presentation

Comp 110 Conditionals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Comp 110Conditionals Instructor: Prasun Dewan

  2. Prerequisite • Types Math .

  3. Conditionals printPassFailStatus(95) Passed printPassFailStatus(25) Failed publicstaticvoidprintPassFailStatus(int score) { if (score < PASS_CUTOFF) System.out.println("FAILED"); else System.out.println("PASSED"); }

  4. If-Else Statement if ( <boolean expression> ) <statement 1>; else <statement 2>;

  5. If-Else Statement <boolean expression> true false <statement 1> <statement 2>

  6. Compound Statement • publicvoidfancyPrintGrade(int score) • { • if (score < PASS_CUTOFF) • { • System.out.println("**************"); • System.out.println("FAILED"); • System.out.println("**************"); • } • else • { • System.out.println("**************"); • System.out.println("PASSED"); • System.out.println("Congratulations!"); • System.out.println("**************"); • } • }

  7. Compound Statement – { } Convention publicvoidfancyPrintGrade(int score) { if (score < PASS_CUTOFF) { System.out.println("**************"); System.out.println("FAILED"); System.out.println("**************"); } else { System.out.println("**************"); System.out.println("PASSED"); System.out.println("Congratulations!"); System.out.println("**************"); } }

  8. Avoiding Code Duplication in If-Else (Edit) • publicvoidfancyPrintGrade(int score) • { • if (score < PASS_CUTOFF) • { • System.out.println("**************"); • System.out.println("FAILED"); • System.out.println("**************"); • } • else • { • System.out.println("**************"); • System.out.println("PASSED"); • System.out.println("Congratulations!"); • System.out.println("**************"); • } • }

  9. Avoiding Code Duplication in If-Else publicvoidfancyPrintGrade(int score) { System.out.println("**************"); if (score < PASS_CUTOFF) System.out.println("FAILED"); else { System.out.println("PASSED"); System.out.println("Congratulations!"); } System.out.println("**************"); }

  10. If Statement if (score == MAX_SCORE) System.out.println ("Perfect Score! Congratulations!"); if (<boolexpr>) <statement>;

  11. If Statement <boolean expression> true false <statement 1>

  12. Else-If • publicstaticchartoLetterGrade (int score) { • if (score >= A_CUTOFF) • return 'A'; • elseif (score >= B_CUTOFF) • return 'B'; • elseif (score >= C_CUTOFF) • return 'C'; • elseif (score >= D_CUTOFF) • return 'D'; • else • return 'F'; • }

  13. Nested If-Else • if (score >= A_CUTOFF) • return 'A'; • else • if (score >= B_CUTOFF) • return 'B'; • else • if (score >= C_CUTOFF) • return 'C'; • else • if (score >= D_CUTOFF) • return 'D'; • else • return 'F'; 

  14. Nested If-Else

More Related