More on Conditionals - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

More on Conditionals

Description:

{ weight = newVal; public static double calculatBMI(double weight, double height) ... Correct Interpretation. Nested If-Else Style Reminder ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 25
Provided by: Prasun2
Learn more at: http://www.cs.unc.edu
Category:

less

Transcript and Presenter's Notes

Title: More on Conditionals


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

2
Four 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()
3
Side 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
4
Alternative to Changing Global Variables
public int increment() counter
return counter
public void incrementCounter()
counter
public int getCounter() return counter

5
If-Else Vs Boolean Expressions
public static boolean hasFailed(int score)
if (score true else return false
6
If-Else Style
public static boolean hasFailed(int score)
return score 7
If-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'
8
Nested 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'
9
Else Branching
10
Then Branching
11
Then 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'
12
Balanced Branching
13
Balanced 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'
14
Nested If-Else Style
  • Linear Branching preferable to Balanced Branches
  • Else Branching Preferable to Then Branching

15
No 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
16
When 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
17
No 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'
18
Equivalent 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'
19
Early Returns in Procedures
public void printLetterGrade(int score) if
(score score") return System.out.println(Letter
Grade toLetterGrade(score))
20
Without Early Return in Procedures
public void printLetterGrade(int score) if
(score else System.out.println(Letter Grade
toLetterGrade(score))
21
Dangling Else
if (score B_CUTOFF) if (score A_CUTOFF)
System.out.println ("excellent!")
else System.out.println ("bad")
22
Dangling Else Matching outermost if
if (score B_CUTOFF) if (score
A_CUTOFF) System.out.println ("excellent!")
else System.out.println ("bad")
23
Dangling Else Matching inner-most if
Correct Interpretation
if (score B_CUTOFF) if (score
A_CUTOFF) System.out.println ("excellent!")
else System.out.println
("bad")
24
Nested If-Else Style Reminder
  • Linear Branching preferable to Balanced Branches
  • Else Branching Preferable to Then Branching
  • Do not have dangling-else ambiguity
Write a Comment
User Comments (0)
About PowerShow.com