c omp 110 c onditionals instructor: jason carter
Post on 19-Dec-2015
221 views
TRANSCRIPT
![Page 1: C OMP 110 C ONDITIONALS Instructor: Jason Carter](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2d5503460f94a0434e/html5/thumbnails/1.jpg)
COMP 110CONDITIONALS
Instructor: Jason Carter
![Page 2: C OMP 110 C ONDITIONALS Instructor: Jason Carter](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2d5503460f94a0434e/html5/thumbnails/2.jpg)
2
MORE ON CONDITIONALS
Miscellaneous (Side effects) Style issues Early returns
![Page 3: C OMP 110 C ONDITIONALS Instructor: Jason Carter](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2d5503460f94a0434e/html5/thumbnails/3.jpg)
3
FOUR KINDS OF METHODS
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)
calculateBMI(70, 1.77)
…
24.57
24.57
getWeight() 70setWeight(77) getWeight() 77
impure function
getWeight() 77
541
readNextNumber ()
5readNextNumber ()
4
impure function with side effects
![Page 4: C OMP 110 C ONDITIONALS Instructor: Jason Carter](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2d5503460f94a0434e/html5/thumbnails/4.jpg)
4
SIDE EFFECTS
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
![Page 5: C OMP 110 C ONDITIONALS Instructor: Jason Carter](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2d5503460f94a0434e/html5/thumbnails/5.jpg)
5
ALTERNATIVE TO CHANGING A GLOBAL VARIABLE
public int increment() { counter++; return counter; }
public void incrementCounter() { counter++; }
public int getCounter() { return counter; }
![Page 6: C OMP 110 C ONDITIONALS Instructor: Jason Carter](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2d5503460f94a0434e/html5/thumbnails/6.jpg)
6
IF-ELSE VS. BOOLEAN EXPRESSIONS
public static boolean hasFailed(int score) {if (score < PASS_CUTOFF)
return true;else
return false;}
![Page 7: C OMP 110 C ONDITIONALS Instructor: Jason Carter](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2d5503460f94a0434e/html5/thumbnails/7.jpg)
7
IF-ELSE STYLE
public static boolean hasFailed(int score) {return score < PASS_CUTOFF;
}
![Page 8: C OMP 110 C ONDITIONALS Instructor: Jason Carter](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2d5503460f94a0434e/html5/thumbnails/8.jpg)
8
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';}
![Page 9: C OMP 110 C ONDITIONALS Instructor: Jason Carter](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2d5503460f94a0434e/html5/thumbnails/9.jpg)
9
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';}
![Page 10: C OMP 110 C ONDITIONALS Instructor: Jason Carter](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2d5503460f94a0434e/html5/thumbnails/10.jpg)
10
ELSE BRANCHING
![Page 11: C OMP 110 C ONDITIONALS Instructor: Jason Carter](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2d5503460f94a0434e/html5/thumbnails/11.jpg)
11
THEN BRANCHING
![Page 12: C OMP 110 C ONDITIONALS Instructor: Jason Carter](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2d5503460f94a0434e/html5/thumbnails/12.jpg)
12
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‘;
elsereturn 'B';
else // score < B_CUTOFFreturn 'C‘;
else // score < C_CUTOFFreturn 'D';
else // score < D_CUTOFFreturn 'F';
}
}
![Page 13: C OMP 110 C ONDITIONALS Instructor: Jason Carter](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2d5503460f94a0434e/html5/thumbnails/13.jpg)
13
BALANCED BRANCHING
![Page 14: C OMP 110 C ONDITIONALS Instructor: Jason Carter](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2d5503460f94a0434e/html5/thumbnails/14.jpg)
14
BALANCED BRANCHING
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';}
![Page 15: C OMP 110 C ONDITIONALS Instructor: Jason Carter](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2d5503460f94a0434e/html5/thumbnails/15.jpg)
15
NESTED IF-ELSE STYLE
Linear branching preferable to balanced branches
Else branching preferable to then branching
![Page 16: C OMP 110 C ONDITIONALS Instructor: Jason Carter](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2d5503460f94a0434e/html5/thumbnails/16.jpg)
16
NO NESTING
public static char toLetterGrade (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;
}
![Page 17: C OMP 110 C ONDITIONALS Instructor: Jason Carter](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2d5503460f94a0434e/html5/thumbnails/17.jpg)
17
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';else if (score >= D_CUTOFF)
result = 'D';else
result = F';return result;
}
![Page 18: C OMP 110 C ONDITIONALS Instructor: Jason Carter](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2d5503460f94a0434e/html5/thumbnails/18.jpg)
18
NO NESTING WITH EARLY RETURNS
public static char toLetterGrade (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';
}
![Page 19: C OMP 110 C ONDITIONALS Instructor: Jason Carter](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2d5503460f94a0434e/html5/thumbnails/19.jpg)
19
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';}
![Page 20: C OMP 110 C ONDITIONALS Instructor: Jason Carter](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2d5503460f94a0434e/html5/thumbnails/20.jpg)
20
EARLY RETURNS IN PROCEDURES
public void printLetterGrade(int score) { if (score < 0) {
System.out.println ("Illegal score");return;
} System.out.println(“Letter Grade:” + toLetterGrade(score”));
}
![Page 21: C OMP 110 C ONDITIONALS Instructor: Jason Carter](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2d5503460f94a0434e/html5/thumbnails/21.jpg)
21
WITHOUT EARLY RETURN IN PROCEDURES
public void printLetterGrade(int score) { if (score < 0)
System.out.println ("Illegal score");else
System.out.println ("Letter Grade: ” + toLetterGrade(score));
}
![Page 22: C OMP 110 C ONDITIONALS Instructor: Jason Carter](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2d5503460f94a0434e/html5/thumbnails/22.jpg)
22
DANGLING ELSE
if (score >= B_CUTOFF)if (score >= A_CUTOFF)
System.out.println ("excellent!");else
System.out.println ("bad");}
Matching if?
![Page 23: C OMP 110 C ONDITIONALS Instructor: Jason Carter](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2d5503460f94a0434e/html5/thumbnails/23.jpg)
23
DANGLING ELSE: MATCHING OUTERMOST IF
if (score >= B_CUTOFF)if (score >= A_CUTOFF)
System.out.println ("excellent!");else
System.out.println ("bad");
![Page 24: C OMP 110 C ONDITIONALS Instructor: Jason Carter](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2d5503460f94a0434e/html5/thumbnails/24.jpg)
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
![Page 25: C OMP 110 C ONDITIONALS Instructor: Jason Carter](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2d5503460f94a0434e/html5/thumbnails/25.jpg)
25
NESTED IF-ELSE STYLE REMINDER
Linear Branching preferable to Balanced Branches
Else Branching Preferable to Then Branching Do not have dangling-else ambiguity