Title: More on Conditionals
1More on Conditionals
- Miscellaneous (Side effects)
- Style issues
- Early returns
2Four Kinds of Methods
procedure
public void setWeight (double newVal)
weight newVal
setWeight(70)
function
pure function
24.57
calculateBMI(70, 1.77)
public static double calculatBMI(double weight,
double height) return
weight/(heightheight)
...
24.57
calculateBMI(70, 1.77)
impure function
getWeight()
70
public int getWeight() return weight
setWeight(77)
getWeight()
77
getWeight()
77
5 4 1
impure function with side effects
readNextNumber()
5
4
public static int readNextNumber()
System.out.println(Next Number") return
Keyboard.readInt()
readNextNumber()
3Side effects
- Effect other than computing return value.
- Printing something
- Reading input
- Changing global variable
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
4Alternative to Changing Global Variables
public int increment() counter
return counter
public void incrementCounter()
counter
public int getCounter() return counter
5If-Else Vs Boolean Expressions
public static boolean hasFailed(int score)
if (score true else return false
6If-Else Style
public static boolean hasFailed(int score)
return score
7If-else redundant test
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'
8Nested if-else
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'
9Else Branching
10Then Branching
11Then Branching
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
C_CUTOFF return 'D' else // score return 'F'
12Balanced Branching
13Balanced Branching
public static char toLetterGrade (int score) if
(score B_CUTOFF) if (score A_CUTOFF) return
'A' else return 'B' else // score (score 'F' else return 'D' else // score
C_CUTOFF return 'C'
14Nested If-Else Style
- Linear Branching preferable to Balanced Branches
- Else Branching Preferable to Then Branching
15No Nesting
public static char toLetterGrade (int score)
char result if (score A_CUTOFF)
result 'A' if ((score
B_CUTOFF)) result 'B' if ((score B_CUTOFF) (score C_CUTOFF)) result
'C' if ((score
D_CUTOFF)) result 'D' if (score D_CUTOFF) result F' return
result
16When Else Branch is Needed
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' if (score D_CUTOFF) result
'D' else result F' return
result
17No Nesting with Early Returns
public static char toLetterGrade (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'
18Equivalent Solution
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'
19Early Returns in Procedures
public void printLetterGrade(int score) if
(score score") return System.out.println(Letter
Grade toLetterGrade(score))
20Without Early Return in Procedures
public void printLetterGrade(int score) if
(score else System.out.println(Letter Grade
toLetterGrade(score))
21Dangling Else
if (score B_CUTOFF) if (score A_CUTOFF)
System.out.println ("excellent!")
else System.out.println ("bad")
22Dangling Else Matching outermost if
if (score B_CUTOFF) if (score
A_CUTOFF) System.out.println ("excellent!")
else System.out.println ("bad")
23Dangling Else Matching inner-most if
Correct Interpretation
if (score B_CUTOFF) if (score
A_CUTOFF) System.out.println ("excellent!")
else System.out.println
("bad")
24Nested If-Else Style Reminder
- Linear Branching preferable to Balanced Branches
- Else Branching Preferable to Then Branching
- Do not have dangling-else ambiguity