1 / 25

Comp 110 Conditionals

Comp 110 Conditionals. Instructor: Jason Carter. More on Conditionals. Miscellaneous (Side effects) Style issues Early returns. Four Kinds of Methods. procedure. public void setWeight (double newVal) { weight = newVal; }. setWeight (70);. . function. pure function.

dougal
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: Jason Carter

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

  3. Four Kinds of Methods procedure publicvoid setWeight (double newVal) { weight = newVal; } setWeight(70);  function pure function public static double calculatBMI( double weight, double height) { return weight/(height*height); } calculateBMI(70, 1.77)  24.57 … calculateBMI(70, 1.77)  24.57 impure function publicintgetWeight() { return weight; } getWeight()  70 setWeight(77)  getWeight()  77 impure function with side effects getWeight()  77 publicstaticint readNextNumber() { System.out.println(”Next Number:"); return Keyboard.readInt(); } 5 readNextNumber ()  5 4 readNextNumber ()  4 1

  4. Side Effects Effect other than computing the return value publicintgetWeight() { System.out.println(“get Weight called” ); return weight; } Printing something publicstaticint readNextNumber() { System.out.println(”Next Number:"); returnnew Keyboard.readInt(); } Reading input publicint increment() { counter++; return counter; } Changing global variable

  5. Alternative to Changing a Global Variable publicint increment() { counter++; return counter; } publicvoidincrementCounter() { counter++; } publicintgetCounter() { return counter; }

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

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

  8. 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'; }

  9. 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'; }

  10. Else Branching

  11. Then Branching

  12. 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'; • } • }

  13. Balanced Branching

  14. 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'; }

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

  16. No Nesting publicstaticchartoLetterGrade (int score) { char result; 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; }

  17. When Else Branch is Needed publicstaticchartoLetterGrade (int score) { char result; 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; }

  18. No Nesting with Early Returns publicstaticchartoLetterGrade (int score) { char result; 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'; }

  19. Equivalent Solution 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'; else return F'; }

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

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

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

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

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

  25. 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