1 / 23

Comp 110 Conditionals

Comp 110 Conditionals. Instructor: Sasa Junuzovic. Prerequisite. Conditionals Loops . More on Conditionals. Miscellaneous (Side effects) Style issues Early returns. If-Else vs. Boolean Expressions. public static boolean hasFailed ( int score) { if (score < PASS_CUTOFF)

keefer
Télécharger la présentation

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: Sasa Junuzovic

  2. Prerequisite • Conditionals Loops .

  3. More on Conditionals • Miscellaneous (Side effects) • Style issues • Early returns

  4. If-Else vs. Boolean Expressions publicstaticbooleanhasFailed(int score) { if (score < PASS_CUTOFF) return true; else return false; }

  5. If-Else Style publicstaticbooleanhasFailed(int score) { return score < PASS_CUTOFF; }

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

  7. Nested If-Else 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'; }

  8. Else Branching

  9. Then Branching

  10. Then Branching • publicstaticchartoLetterGrade (int score) { • if (score >= D_CUTOFF) • if (score >= C_CUTOFF) • if (score >= B_CUTOFF) • if (score >= A_CUTOFF) • return 'A‘; • else • return 'B'; • else// score < B_CUTOFF • return 'C‘; • else// score < C_CUTOFF • return 'D'; • else// score < D_CUTOFF • return 'F'; • }

  11. Balanced Branching

  12. Balanced Branching publicstaticchartoLetterGrade (int score) { if (score >= B_CUTOFF) if (score >= A_CUTOFF) return 'A'; else return 'B'; else // score < B_CUTOFF if (score < C_CUTOFF) if (score < D_CUTOFF) return 'F'; else return 'D'; else // score >= C_CUTOFF return 'C'; }

  13. Nested If-Else Style • Linear branching preferable to balanced branches • Else branching preferable to then branching

  14. No Nesting publicstaticchartoLetterGrade (int score) { char result = ‘I’; if (score >= A_CUTOFF) result = 'A'; if ((score < A_CUTOFF) && (score >= B_CUTOFF)) result = 'B'; if ((score < B_CUTOFF) && (score >= C_CUTOFF)) result = 'C'; if ((score < C_CUTOFF) && (score >= D_CUTOFF)) result = 'D'; if (score < D_CUTOFF) result = ‘F'; return result; }

  15. When Else Branch is Needed publicstaticchartoLetterGrade (int score) { char result = ‘I’; if (score >= A_CUTOFF) result = 'A'; else if (score >= B_CUTOFF) result = 'B'; else if (score >= C_CUTOFF) result = 'C'; else if (score >= D_CUTOFF) result = 'D'; else result = ‘F'; return result; }

  16. No Nesting with Early Returns publicstaticchartoLetterGrade (int score) { 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'; return F'; }

  17. Equivalent Solution publicstaticchartoLetterGrade (int score) { if (score >= A_CUTOFF) return 'A'; if (score >= B_CUTOFF) return 'B'; if (score >= C_CUTOFF) return 'C'; if (score >= D_CUTOFF) return 'D'; return ‘F'; }

  18. Early Returns in Procedures publicvoidprintLetterGrade(int score) { if (score < 0) { System.out.println ("Illegal score"); return; } System.out.println(“Letter Grade:” + toLetterGrade(score”)); }

  19. Without Early Return in Procedures publicvoidprintLetterGrade(int score) { if (score < 0) System.out.println ("Illegal score"); else System.out.println ( "Letter Grade: ” + toLetterGrade(score)); }

  20. Dangling Else if (score >= B_CUTOFF) if (score >= A_CUTOFF) System.out.println ("excellent!"); else System.out.println ("bad"); } Matching if?

  21. Dangling Else: Matching Outermost If if (score >= B_CUTOFF) if (score >= A_CUTOFF) System.out.println ("excellent!"); else System.out.println ("bad");

  22. Dangling Else: Matching Innermost If if (score >= B_CUTOFF) if (score >= A_CUTOFF) System.out.println ("excellent!"); else System.out.println ("bad"); Correct interpretation

  23. Nested If-Else Style Reminder • Linear Branching preferable to Balanced Branches • Else Branching Preferable to Then Branching • Do not have dangling-else ambiguity

More Related