250 likes | 390 Views
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.
E N D
Comp 110Conditionals Instructor: Jason Carter
More on Conditionals • Miscellaneous (Side effects) • Style issues • Early returns
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
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
Alternative to Changing a Global Variable publicint increment() { counter++; return counter; } publicvoidincrementCounter() { counter++; } publicintgetCounter() { return counter; }
If-Else vs. Boolean Expressions publicstaticbooleanhasFailed(int score) { if (score < PASS_CUTOFF) return true; else return false; }
If-Else Style publicstaticbooleanhasFailed(int score) { return score < PASS_CUTOFF; }
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'; }
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'; }
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'; • } • }
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'; }
Nested If-Else Style • Linear branching preferable to balanced branches • Else branching preferable to then branching
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; }
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; }
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'; }
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'; }
Early Returns in Procedures publicvoidprintLetterGrade(int score) { if (score < 0) { System.out.println ("Illegal score"); return; } System.out.println(“Letter Grade:” + toLetterGrade(score”)); }
Without Early Return in Procedures publicvoidprintLetterGrade(int score) { if (score < 0) System.out.println ("Illegal score"); else System.out.println ( "Letter Grade: ” + toLetterGrade(score)); }
Dangling Else if (score >= B_CUTOFF) if (score >= A_CUTOFF) System.out.println ("excellent!"); else System.out.println ("bad"); } Matching if?
Dangling Else: Matching Outermost If if (score >= B_CUTOFF) if (score >= A_CUTOFF) System.out.println ("excellent!"); else System.out.println ("bad");
Dangling Else: Matching Innermost If if (score >= B_CUTOFF) if (score >= A_CUTOFF) System.out.println ("excellent!"); else System.out.println ("bad"); Correct interpretation
Nested If-Else Style Reminder • Linear Branching preferable to Balanced Branches • Else Branching Preferable to Then Branching • Do not have dangling-else ambiguity