Decision making Last week Logical and boolean values
Decision making
Last week Logical and boolean values Review Test Quiz
This week Decision making Problem solving
Decision making n Possibilities n if statement n if-else-if idiom n switch statement
Basic if statement n n Syntax if (Expression) Action If the Expression is true then execute Action Expression Regardless whether Action is executed, execution continues with the statement following the if statement true An Action is either a single statement or a group of statements within braces (block) n For us, it will always be a block Action false
The if-else statement n Syntax if (Expression) Action 1 else Action 2 n n n If Expression is true then execute Action 1 otherwise execute Action 2 Execution continues with the statement following the if-else statement An action is either a single statement or a block n For us, it will always be a block Expression true false Action 1 Action 2
Problem n Given two values, report the minimum n How? n Guess that the first value is the minimum and update your guess if the second value is the smaller of the two n Compare the two numbers, whichever is smaller is the minimum
Determining the minimum n Guess and update if necessary public static int min (int v 1, int v 2) { int result = v 1; if ( v 2 < v 1 ) { result = v 2; } return result; }
Determining the minimum Is v 2 less than v 1? Yes it is. So update the guess with correct value v 2 < v 1 false true result = v 2 Either way when the next statement is reached, result is correctly set No it’s not. So the guess v 1 was correct , and no update is needed
Determining the minimum n Guess and update if necessary public static int min (int v 1, int v 2) { int result = v 1; if ( v 2 < v 1 ) { result = v 2; } return result; } Consider Scanner stdin = new Scanner(System. in); System. out. print(“Two integers: "); int n 1 = stdin. next. Int(); int n 2 = stdin. next. Int(); System. out. println( min(n 1, n 2) );
Why we always use braces n What is the output? int m = 15; int n = 10; if (m < n) ++m; ++n; System. out. println(" m = " + m + " n = " n);
Problem n Given two values, report the minimum n How? n Guess that the first value is the minimum and update your guess if the second value is the smaller of the two n Compare the two numbers, whichever is smaller is the minimum
Determining the minimum n Compare and choose smaller public static int min (int v 1, int v 2) { int result; if ( v 2 < v 1 ) { result = v 2; } else { result = v 1; } return result; } Not initialized! Additional code must guarantee correct initialization so that the return makes sense
Determining the minimum n Compare and choose smaller public static int min (int v 1, int v 2) { int result; if ( v 2 < v 1 ) { result = v 2; } else { result = v 1; } return result; } Correct initialization based on the parameter with the lesser value
Determining the minimum Is v 2 less than v 1? Yes it so. So v 2 is the result v 2 < v 1 true result = v 2 Either way, result is correctly set No it’s not. So v 1 is the result false result = v 1
Determining the minimum n Compare and choose smaller public static int min (int v 1, int v 2) { int result; if ( v 2 < v 1 ) { result = v 2; } else { result = v 1; } return result; } Consider Scanner stdin = new Scanner(System. in); System. out. print(“Two integers: "); int n 1 = stdin. next. Int(); int n 2 = stdin. next. Int(); System. out. println( min(n 1, n 2) );
Problem n Given three values, report the minimum n Problem solving possibilities n Serial determination n Determine if first value is minimum n Determine if second value is minimum n Determine if third value is minimum n Case elimination n Determine the smaller of the first two values and determine whether it or the third value is the smaller
Serial implementation n Does not compile public static int min (int v 1, int v 2, int v 3) { if ( ( v 1 <= v 2 ) && ( v 1 <= v 3 ) ) { int result = v 1; } if ( ( v 2 <= v 1 ) && ( v 2 <= v 3 ) ) { int result = v 2; } if ( ( v 3 <= v 1) && ( v 3 <= v 2 ) { int result = v 3; } return result; } Case analysis ensures that a variable named result is defined and initialized with the minimum value There are three variables named result. They are local to a different if statements
Serial implementation n Logically correct but still does not compile public static int min (int v 1, int v 2, int v 3) { int result; if ( ( v 1 <= v 2 result = v 1; } ) && ( v 1 <= v 3 ) ) { if ( ( v 2 <= v 1 ) && ( v 2 <= v 3 ) ) { result = v 2; } if ( ( v 3 <= v 1) && ( v 3 <= v 2 ) { result = v 3; } return result; } The compiler does not ‘know’ that result is guaranteed a value – it does not consider the semantics, only the syntax
Serial implementation n Correct and compiles public static int min (int v 1, int v 2, int v 3) { int result = 0; if ( ( v 1 <= v 2 result = v 1; } ) && ( v 1 <= v 3 ) ) { if ( ( v 2 <= v 1 ) && ( v 2 <= v 3 ) ) { result = v 2; } if ( ( v 3 <= v 1) && ( v 3 <= v 2 ) { result = v 3; } return result; } Initialization makes Java ‘happy’ Case analysis ensures result is correctly set
Another serial implementation n Again logically correct but does not compile public static int min (int v 1, int v 2, int v 3) { if ( ( v 1 <= v 2 return v 1; } ) && ( v 1 <= v 3 ) ) { if ( ( v 2 <= v 1 ) && ( v 2 <= v 3 ) ) { return v 2; } So what can we do – guarantee a return at the end if ( ( v 3 <= v 1) && ( v 3 <= v 2 ) { return v 3; } } The compiler does not ‘know’ a return is guaranteed – it does not consider the semantics, only the syntax
Another serial implementation n Correct and compiles public static int min (int v 1, int v 2, int v 3) { if ( ( v 1 <= v 2 return v 1; } ) && ( v 1 <= v 3 ) ) { if ( ( v 2 <= v 1 ) && ( v 2 <= v 3 ) ) { return v 2; } So what can we do – guarantee a return at the end return v 3; Compiler ‘knows’ a return is guaranteed }
Another serial implementation n Correct and compiles public static int min (int v 1, int v 2, int v 3) { if ( ( v 1 <= v 2 return v 1; } ) && ( v 1 <= v 3 ) ) { So what can we else { do – guarantee if ( ( v 2 <= v 1 ) && ( v 2 <= v 3 ) ) { a return at the return v 2; end } else { return v 3; } } } Compiler ‘knows’ a return is guaranteed
Another serial implementation n Correct and compiles public static int min (int v 1, int v 2, int v 3) { if ( ( v 1 <= v 2 return v 1; } ) && ( v 1 <= v 3 ) ) { else { if ( ( v 2 <= v 1 ) && ( v 2 <= v 3 ) ) { return v 2; } Can we make the code easier to understand else { return v 3; } } } Compiler ‘knows’ a return is guaranteed
Another serial implementation n Correct and compiles public static int min (int v 1, int v 2, int v 3) { if ( ( v 1 <= v 2 return v 1; } ) && ( v 1 <= v 3 ) ) { else { if ( ( v 2 <= v 1 ) && ( v 2 <= v 3 ) ) { return v 2; } Are these braces necessary? else { return v 3; } } } Compiler ‘knows’ a return is guaranteed
Another serial implementation n Correct and compiles public static int min (int v 1, int v 2, int v 3) { if ( ( v 1 <= v 2 return v 1; } ) && ( v 1 <= v 3 ) ) { else { if ( ( v 2 <= v 1 ) && ( v 2 <= v 3 ) ) { return v 2; } Are these braces necessary? No! else { return v 3; } } } Compiler ‘knows’ a return is guaranteed
Another serial implementation n Correct and compiles public static int min (int v 1, int v 2, int v 3) { if ( ( v 1 <= v 2 return v 1; } ) && ( v 1 <= v 3 ) ) { else if ( ( v 2 <= v 1 ) && ( v 2 <= v 3 ) ) { return v 2; } Are these braces necessary? No! So let’s remove them else { return v 3; } } Compiler ‘knows’ a return is guaranteed
Another serial implementation n Correct and compiles public static int min (int v 1, int v 2, int v 3) { if ( ( v 1 <= v 2 return v 1; } ) && ( v 1 <= v 3 ) ) { else if ( ( v 2 <= v 1 ) && ( v 2 <= v 3 ) ) { return v 2; } else { return v 3; } } Compiler ‘knows’ a return is guaranteed
Another serial implementation n Correct and compiles public static int min (int v 1, int v 2, int v 3) { if ( ( v 1 <= v 2 return v 1; } ) && ( v 1 <= v 3 ) ) { else if ( ( v 2 <= v 1 ) && ( v 2 <= v 3 ) ) { return v 2; } else { return v 3; } } Does changing the whitespace affect the solution? No! Compiler ‘knows’ a return is guaranteed
Another serial implementation n Correct and compiles public static int min (int v 1, int v 2, int v 3) { if ( ( v 1 <= v 2 return v 1; } ) && ( v 1 <= v 3 ) ) { else if ( ( v 2 <= v 1 ) && ( v 2 <= v 3 ) ) { return v 2; } else { return v 3; } } Does changing the whitespace affect the solution? No! So let’s roll Compiler ‘knows’ a return is guaranteed
Another serial implementation n Correct and compiles public static int min (int v 1, int v 2, int v 3) { if ( ( v 1 <= v 2 return v 1; } ) && ( v 1 <= v 3 ) ) { else if ( ( v 2 <= v 1 ) && ( v 2 <= v 3 ) ) { return v 2; } else { return v 3; } }
Another serial implementation n Correct and compiles public static int min (int v 1, int v 2, int v 3) { if ( ( v 1 <= v 2 return v 1; } ) && ( v 1 <= v 3 ) ) { else if ( ( v 2 <= v 1 ) && ( v 2 <= v 3 ) ) { return v 2; } else { return v 3; } }
Another serial implementation n Correct and compiles public static int min (int v 1, int v 2, int v 3) { if ( ( v 1 <= v 2 return v 1; } ) && ( v 1 <= v 3 ) ) { else if ( ( v 2 <= v 1 ) && ( v 2 <= v 3 ) ) { return v 2; } else { return v 3; } }
Another serial implementation n Correct and compiles public static int min (int v 1, int v 2, int v 3) { if ( ( v 1 <= v 2 return v 1; } ) && ( v 1 <= v 3 ) ) { else if ( ( v 2 <= v 1 ) && ( v 2 <= v 3 ) ) { return v 2; } else { return v 3; } }
Another serial implementation n Correct and compiles public static int min (int v 1, int v 2, int v 3) { if ( ( v 1 <= v 2 return v 1; } ) && ( v 1 <= v 3 ) ) { else if ( ( v 2 <= v 1 ) && ( v 2 <= v 3 ) ) { return v 2; } else { return v 3; } }
Another serial implementation n Correct and compiles public static int min (int v 1, int v 2, int v 3) { if ( ( v 1 <= v 2 return v 1; } ) && ( v 1 <= v 3 ) ) { else if ( ( v 2 <= v 1 ) && ( v 2 <= v 3 ) ) { return v 2; } else { return v 3; } }
Another serial implementation n Correct and compiles public static int min (int v 1, int v 2, int v 3) { if ( ( v 1 <= v 2 return v 1; } ) && ( v 1 <= v 3 ) ) { else if ( ( v 2 <= v 1 ) && ( v 2 <= v 3 ) ) { return v 2; } else { return v 3; } }
Another serial implementation n Correct and compiles public static int min (int v 1, int v 2, int v 3) { if ( ( v 1 <= v 2 return v 1; } ) && ( v 1 <= v 3 ) ) { else if ( ( v 2 <= v 1 ) && ( v 2 <= v 3 ) ) { return v 2; } else { return v 3; } }
Another serial implementation n Correct and compiles public static int min (int v 1, int v 2, int v 3) { if ( ( v 1 <= v 2 return v 1; } ) && ( v 1 <= v 3 ) ) { else if ( ( v 2 <= v 1 ) && ( v 2 <= v 3 ) ) { return v 2; } else { return v 3; } }
Another serial implementation n Correct and compiles public static int min (int v 1, int v 2, int v 3) { if ( ( v 1 <= v 2 return v 1; } ) && ( v 1 <= v 3 ) ) { else if ( ( v 2 <= v 1 ) && ( v 2 <= v 3 ) ) { return v 2; } else { return v 3; } } We call it the if-else-if idiom Used when there are different actions to take depending upon which conditions true Text uses it in the sorting of three numbers
Another serial implementation n Correct and compiles public static int min (int v 1, int v 2, int v 3) { if ( ( v 1 <= v 2 return v 1; } ) && ( v 1 <= v 3 ) ) { else if ( ( v 2 <= v 1 ) && ( v 2 <= v 3 ) ) { return v 2; } else { return v 3; } } Do we really need the ( v 2 <= v 1 ) comparison?
Another serial implementation n Correct and compiles public static int min (int v 1, int v 2, int v 3) { if ( ( v 1 <= v 2 return v 1; } ) && ( v 1 <= v 3 ) ) { else if ( ( v 2 <= v 1 ) && ( v 2 <= v 3 ) ) { return v 2; } else { return v 3; } } Do we really need the ( v 2 <= v 1 ) comparison? No. It’s either v 2 or v 3 that can be the min at this point, so comparing v 2 and v 3 is all we need
Another serial implementation n Correct and compiles public static int min (int v 1, int v 2, int v 3) { if ( ( v 1 <= v 2 return v 1; } ) && ( v 1 <= v 3 ) ) { else if ( v 2 <= v 3 ) { return v 2; } else { return v 3; } } Do we really need the ( v 2 <= v 1 ) comparison? No. It’s either v 2 or v 3 that can be the min at this point, so comparing v 2 and v 3 is all we need
Another serial implementation n Correct and compiles public static int min (int v 1, int v 2, int v 3) { if ( ( v 1 <= v 2 return v 1; } ) && ( v 2 <= v 3 ) ) { else if ( v 2 <= v 3 ) { return v 2; } else { return v 3; } } How many comparisons are made? Does the number depend upon the values of v 1, v 2, and v 3? Let’s try to do better – each comparison whether its true or false gives us information about the values
Problem n Given three values, report the minimum n Problem solving possibilities n Serial determination n Determine if first value is minimum n Determine if second value is minimum n Determine if third value is minimum n Case elimination n Determine the smaller of the first two values and determine whether it or the third value is the smaller
Case elimination public static int min (int v 1, int v 2, int v 3) { if ( v 1 <= v 2 ) { if ( v 1 <= v 3 ) { return v 1; } else { return v 3; } } else if ( v 2 <= v 3 ) { return v 2; } else { return v 3; } }
- Slides: 46