460 likes | 470 Views
Enhance your decision-making skills with logical and boolean values in this test review covering if-else statements, switch statements, and problem-solving scenarios.
E N D
Last week Logical and boolean values Review Test Quiz
This week Decision making Problem solving
Decision making • Possibilities • if statement • if-else statement • if-else-if idiom • switch statement
Expression true false Action Basic if statement • Syntax if (Expression) Action • If the Expression is true then execute Action • Regardless whether Action is executed, execution continues with the statement following the if statement • An Action is either a single statement or a group of statements within braces (block) • For us, it will always be a block
Expression false true Action1 Action2 The if-else statement • Syntax if (Expression) Action1elseAction2 • If Expression is true thenexecute Action1otherwiseexecute Action2 • Execution continues with thestatement following the if-elsestatement • An action is either a singlestatement or a block • For us, it will always be a block
Problem • Given two values, report the minimum • How? • Guess that the first value is the minimum and update your guess if the second value is the smaller of the two • Compare the two numbers, whichever is smaller is the minimum
Determining the minimum • Guess and update if necessary public static int min (int v1, int v2) { int result = v1; if ( v2 < v1 ) { result = v2; } return result; }
Determining the minimum Is v2 less than v1? Yes it is. So update the guess with correct valuev2 v2 < v1 false true No it’s not. So the guess v1 was correct , and no update is needed result = v2 Either way when the nextstatement is reached, result is correctly set
Determining the minimum • Guess and update if necessary public static int min (int v1, int v2) { int result = v1; if ( v2 < v1 ) { result = v2; } return result; } Consider Scanner stdin = new Scanner(System.in);System.out.print(“Two integers: ");int n1 = stdin.nextInt();int n2 = stdin.nextInt();System.out.println( min(n1, n2) );
Why we always use braces • What is the output? int m = 15; int n = 10; if (m < n) ++m; ++n; System.out.println(" m = " + m + " n = " n);
Problem • Given two values, report the minimum • How? • Guess that the first value is the minimum and update your guess if the second value is the smaller of the two • Compare the two numbers, whichever is smaller is the minimum
Not initialized! Additional code must guarantee correct initialization so that the return makes sense Determining the minimum • Compare and choose smaller public static int min (int v1, int v2) { int result; if ( v2 < v1 ) { result = v2; } else { result = v1; } return result; }
Correct initialization based on the parameter with the lesser value Determining the minimum • Compare and choose smaller public static int min (int v1, int v2) { int result; if ( v2 < v1 ) { result = v2; } else { result = v1; } return result; }
v2 < v1 false true result = v2 result = v1 Determining the minimum Is v2 less than v1? No it’s not. So v1 is the result Yes it so. So v2 is the result Either way, result is correctly set
Determining the minimum • Compare and choose smaller public static int min (int v1, int v2) { int result; if ( v2 < v1 ) { result = v2; } else { result = v1; } return result; } Consider Scanner stdin = newScanner(System.in);System.out.print(“Two integers: ");int n1 = stdin.nextInt();int n2 = stdin.nextInt();System.out.println( min(n1, n2) );
Problem • Given three values, report the minimum • Problem solving possibilities • Serial determination • Determine if first value is minimum • Determine if second value is minimum • Determine if third value is minimum • Case elimination • Determine the smaller of the first two values and determine whether it or the third value is the smaller
Serial implementation • Does not compile public static int min (int v1, int v2, int v3) { if ( ( v1 <= v2 ) && ( v1 <= v3 ) ) { int result = v1; } if ( ( v2 <= v1 ) && ( v2 <= v3 ) ) { int result = v2; } if ( ( v3 <= v1) && ( v3 <= v2 ) { int result = v3; } 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 • Logically correct but still does not compile public static int min (int v1, int v2, int v3) { int result; if ( ( v1 <= v2 ) && ( v1 <= v3 ) ) { result = v1; } if ( ( v2 <= v1 ) && ( v2 <= v3 ) ) { result = v2; } if ( ( v3 <= v1) && ( v3 <= v2 ) { result = v3; } return result; } The compiler does not ‘know’ that result is guaranteed a value – it does not consider the semantics, only the syntax
Serial implementation • Correct and compiles public static int min (int v1, int v2, int v3) { int result = 0; if ( ( v1 <= v2 ) && ( v1 <= v3 ) ) { result = v1; } if ( ( v2 <= v1 ) && ( v2 <= v3 ) ) { result = v2; } if ( ( v3 <= v1) && ( v3 <= v2 ) { result = v3; } return result; } Initialization makes Java ‘happy’ Case analysis ensures result is correctly set
Another serial implementation • Again logically correct but does not compile public static int min (int v1, int v2, int v3) { if ( ( v1 <= v2 ) && ( v1 <= v3 ) ) { return v1; } if ( ( v2 <= v1 ) && ( v2 <= v3 ) ) { return v2; } if ( ( v3 <= v1) && ( v3 <= v2 ) { return v3; } } So what can we do – guarantee a return at the end The compiler does not ‘know’ a return is guaranteed – it does not consider the semantics, only the syntax
Another serial implementation • Correct and compiles public static int min (int v1, int v2, int v3) { if ( ( v1 <= v2 ) && ( v1 <= v3 ) ) { return v1; } if ( ( v2 <= v1 ) && ( v2 <= v3 ) ) { return v2; } return v3; } So what can we do – guarantee a return at the end Compiler ‘knows’ a return is guaranteed
Another serial implementation • Correct and compiles public static int min (int v1, int v2, int v3) { if ( ( v1 <= v2 ) && ( v1 <= v3 ) ) { return v1; } else { if ( ( v2 <= v1 ) && ( v2 <= v3 ) ) { return v2; } else { return v3; } } } So what can we do – guarantee a return at the end Compiler ‘knows’ a return is guaranteed
Another serial implementation • Correct and compiles public static int min (int v1, int v2, int v3) { if ( ( v1 <= v2 ) && ( v1 <= v3 ) ) { return v1; } else { if ( ( v2 <= v1 ) && ( v2 <= v3 ) ) { return v2; } else { return v3; } } } Can we make the code easier to understand Compiler ‘knows’ a return is guaranteed
Another serial implementation • Correct and compiles public static int min (int v1, int v2, int v3) { if ( ( v1 <= v2 ) && ( v1 <= v3 ) ) { return v1; } else { if ( ( v2 <= v1 ) && ( v2 <= v3 ) ) { return v2; } else { return v3; } } } Are these braces necessary? Compiler ‘knows’ a return is guaranteed
Another serial implementation • Correct and compiles public static int min (int v1, int v2, int v3) { if ( ( v1 <= v2 ) && ( v1 <= v3 ) ) { return v1; } else { if ( ( v2 <= v1 ) && ( v2 <= v3 ) ) { return v2; } else { return v3; } } } Are these braces necessary? No! Compiler ‘knows’ a return is guaranteed
Another serial implementation • Correct and compiles public static int min (int v1, int v2, int v3) { if ( ( v1 <= v2 ) && ( v1 <= v3 ) ) { return v1; } else if ( ( v2 <= v1 ) && ( v2 <= v3 ) ) { return v2; } else { return v3; } } Are these braces necessary? No! So let’s remove them Compiler ‘knows’ a return is guaranteed
Another serial implementation • Correct and compiles public static int min (int v1, int v2, int v3) { if ( ( v1 <= v2 ) && ( v1 <= v3 ) ) { return v1; } else if ( ( v2 <= v1 ) && ( v2 <= v3 ) ) { return v2; } else { return v3; } } Compiler ‘knows’ a return is guaranteed
Another serial implementation • Correct and compiles public static int min (int v1, int v2, int v3) { if ( ( v1 <= v2 ) && ( v1 <= v3 ) ) { return v1; } else if ( ( v2 <= v1 ) && ( v2 <= v3 ) ) { return v2; } else { return v3; } } Does changing the whitespace affect the solution? No! Compiler ‘knows’ a return is guaranteed
Another serial implementation • Correct and compiles public static int min (int v1, int v2, int v3) { if ( ( v1 <= v2 ) && ( v1 <= v3 ) ) { return v1; } else if ( ( v2 <= v1 ) && ( v2 <= v3 ) ) { return v2; } else { return v3; } } Does changing the whitespace affect the solution? No! So let’s roll Compiler ‘knows’ a return is guaranteed
Another serial implementation • Correct and compiles public static int min (int v1, int v2, int v3) { if ( ( v1 <= v2 ) && ( v1 <= v3 ) ) { return v1; } else if ( ( v2 <= v1 ) && ( v2 <= v3 ) ) { return v2; } else { return v3; } }
Another serial implementation • Correct and compiles public static int min (int v1, int v2, int v3) { if ( ( v1 <= v2 ) && ( v1 <= v3 ) ) { return v1; } else if ( ( v2 <= v1 ) && ( v2 <= v3 ) ) { return v2; } else { return v3; } }
Another serial implementation • Correct and compiles public static int min (int v1, int v2, int v3) { if ( ( v1 <= v2 ) && ( v1 <= v3 ) ) { return v1; } else if ( ( v2 <= v1 ) && ( v2 <= v3 ) ) { return v2; } else { return v3; } }
Another serial implementation • Correct and compiles public static int min (int v1, int v2, int v3) { if ( ( v1 <= v2 ) && ( v1 <= v3 ) ) { return v1; } else if ( ( v2 <= v1 ) && ( v2 <= v3 ) ) { return v2; } else { return v3; } }
Another serial implementation • Correct and compiles public static int min (int v1, int v2, int v3) { if ( ( v1 <= v2 ) && ( v1 <= v3 ) ) { return v1; } else if ( ( v2 <= v1 ) && ( v2 <= v3 ) ) { return v2; } else { return v3; } }
Another serial implementation • Correct and compiles public static int min (int v1, int v2, int v3) { if ( ( v1 <= v2 ) && ( v1 <= v3 ) ) { return v1; } else if ( ( v2 <= v1 ) && ( v2 <= v3 ) ) { return v2; } else { return v3; } }
Another serial implementation • Correct and compiles public static int min (int v1, int v2, int v3) { if ( ( v1 <= v2 ) && ( v1 <= v3 ) ) { return v1; } else if ( ( v2 <= v1 ) && ( v2 <= v3 ) ) { return v2; } else { return v3; } }
Another serial implementation • Correct and compiles public static int min (int v1, int v2, int v3) { if ( ( v1 <= v2 ) && ( v1 <= v3 ) ) { return v1; } else if ( ( v2 <= v1 ) && ( v2 <= v3 ) ) { return v2; } else { return v3; } }
Another serial implementation • Correct and compiles public static int min (int v1, int v2, int v3) { if ( ( v1 <= v2 ) && ( v1 <= v3 ) ) { return v1; } else if ( ( v2 <= v1 ) && ( v2 <= v3 ) ) { return v2; } else { return v3; } }
Another serial implementation • Correct and compiles public static int min (int v1, int v2, int v3) { if ( ( v1 <= v2 ) && ( v1 <= v3 ) ) { return v1; } else if ( ( v2 <= v1 ) && ( v2 <= v3 ) ) { return v2; } else { return v3; } } 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 • Correct and compiles public static int min (int v1, int v2, int v3) { if ( ( v1 <= v2 ) && ( v1 <= v3 ) ) { return v1; } else if ( ( v2 <= v1 ) && ( v2 <= v3 ) ) { return v2; } else { return v3; } } Do we really need the ( v2 <= v1 ) comparison?
Another serial implementation • Correct and compiles public static int min (int v1, int v2, int v3) { if ( ( v1 <= v2 ) && ( v1 <= v3 ) ) { return v1; } else if ( ( v2 <= v1 ) && ( v2 <= v3 ) ) { return v2; } else { return v3; } } Do we really need the ( v2 <= v1 ) comparison? No. It’s either v2 or v3 that can be the min at this point, so comparing v2 and v3 is all we need
Another serial implementation • Correct and compiles public static int min (int v1, int v2, int v3) { if ( ( v1 <= v2 ) && ( v1 <= v3 ) ) { return v1; } else if ( v2 <= v3 ) { return v2; } else { return v3; } } Do we really need the ( v2 <= v1 ) comparison? No. It’s either v2 or v3 that can be the min at this point, so comparing v2 and v3 is all we need
Another serial implementation • Correct and compiles public static int min (int v1, int v2, int v3) { if ( ( v1 <= v2 ) && ( v2 <= v3 ) ) { return v1; } else if ( v2 <= v3 ) { return v2; } else { return v3; } } How many comparisons are made? Does the number depend upon the values of v1, v2, and v3? Let’s try to do better – each comparison whether its true or false gives us information about the values
Problem • Given three values, report the minimum • Problem solving possibilities • Serial determination • Determine if first value is minimum • Determine if second value is minimum • Determine if third value is minimum • Case elimination • 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 v1, int v2, int v3) { if ( v1 <= v2 ) { if ( v1 <= v3 ) { return v1; } else { return v3; } } else if ( v2 <= v3 ) { return v2; } else { return v3; } } else if ( v2 <= v3 ) {