Presentation is loading. Please wait.

Presentation is loading. Please wait.

C OMP 110 C ONDITIONALS Instructor: Jason Carter.

Similar presentations


Presentation on theme: "C OMP 110 C ONDITIONALS Instructor: Jason Carter."— Presentation transcript:

1 C OMP 110 C ONDITIONALS Instructor: Jason Carter

2 2 M ORE ON C ONDITIONALS Miscellaneous (Side effects) Style issues Early returns

3 3 F OUR K INDS OF M ETHODS public void setWeight (double newVal) { weight = newVal; } public static double calculatBMI( double weight, double height) { return weight/(height*height); } public int getWeight() { return weight; } public static int readNextNumber() { System.out.println(”Next Number:"); return Keyboard.readInt(); } procedure functionpure function setWeight(70); calculateBMI(70, 1.77) …   24.57 getWeight()  70 setWeight(77)  getWeight()  77 impure function getWeight()  77 5 4 1 readNextNumber ()  5 readNextNumber ()  4 impure function with side effects

4 4 S IDE E FFECTS public int getWeight() { System.out.println(“get Weight called” ); return weight; } public static int readNextNumber() { System.out.println(”Next Number:"); return new Keyboard.readInt(); } public int increment() { counter++; return counter; } Effect other than computing the return value Printing something Reading input Changing global variable

5 5 A LTERNATIVE TO C HANGING A G LOBAL V ARIABLE public int increment() { counter++; return counter; } public void incrementCounter() { counter++; } public int getCounter() { return counter; }

6 6 I F -E LSE VS. B OOLEAN E XPRESSIONS public static boolean hasFailed( int score) { if (score < PASS_CUTOFF) return true; else return false; }

7 7 I F -E LSE S TYLE public static boolean hasFailed( int score) { return score < PASS_CUTOFF; }

8 8 I F -E LSE : R EDUNDANT T EST public static char toLetterGrade ( int score) { if ((score >= A_CUTOFF == true) return 'A'; else if (score >= B_CUTOFF == true) return 'B'; else if (score >= C_CUTOFF == true) return 'C'; else if (score >= D_CUTOFF == true) return 'D'; else return 'F'; }

9 9 N ESTED I F -E LSE public static char toLetterGrade ( 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'; }

10 10 E LSE B RANCHING

11 11 T HEN B RANCHING

12 12 T HEN B RANCHING public static char toLetterGrade ( 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 13 B ALANCED B RANCHING

14 14 B ALANCED B RANCHING public static char toLetterGrade ( 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 15 N ESTED I F -E LSE S TYLE Linear branching preferable to balanced branches Else branching preferable to then branching

16 16 N O N ESTING public static char toLetterGrade ( int score) { char result; if (score >= A_CUTOFF) result = 'A'; if ((score = B_CUTOFF)) result = 'B'; if ((score = C_CUTOFF)) result = 'C'; if ((score = D_CUTOFF)) result = 'D'; if (score < D_CUTOFF) result = F'; return result; }

17 17 W HEN E LSE B RANCH IS N EEDED public static char toLetterGrade ( 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 18 N O N ESTING WITH E ARLY R ETURNS public static char toLetterGrade ( int score) { char result; if (score >= A_CUTOFF) return 'A'; e lse if (score >= B_CUTOFF) return 'B'; else if (score >= C_CUTOFF) return 'C'; else if (score >= D_CUTOFF) return 'D'; return F'; }

19 19 E QUIVALENT S OLUTION public static char toLetterGrade ( 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 20 E ARLY R ETURNS IN P ROCEDURES public void printLetterGrade(int score) { if (score < 0) { System.out.println ("Illegal score"); return ; } System.out.println(“Letter Grade:” + toLetterGrade(score”)); }

21 21 W ITHOUT E ARLY R ETURN IN P ROCEDURES public void printLetterGrade(int score) { if (score < 0) System.out.println ("Illegal score"); else System.out.println ( "Letter Grade: ” + toLetterGrade(score)); }

22 22 D ANGLING E LSE if (score >= B_CUTOFF) if (score >= A_CUTOFF) System.out.println ("excellent!"); else System.out.println ("bad"); } Matching if?

23 23 D ANGLING E LSE : M ATCHING O UTERMOST I F if (score >= B_CUTOFF) if (score >= A_CUTOFF) System.out.println ("excellent!"); else System.out.println ("bad");

24 24 D ANGLING E LSE : M ATCHING I NNERMOST I F if (score >= B_CUTOFF) if (score >= A_CUTOFF) System.out.println ("excellent!"); else System.out.println ("bad"); Correct interpretation

25 25 N ESTED I F -E LSE S TYLE R EMINDER Linear Branching preferable to Balanced Branches Else Branching Preferable to Then Branching Do not have dangling-else ambiguity


Download ppt "C OMP 110 C ONDITIONALS Instructor: Jason Carter."

Similar presentations


Ads by Google