1 / 46

Decision making

Enhance your decision-making skills with logical and boolean values in this test review covering if-else statements, switch statements, and problem-solving scenarios.

mmoon
Download Presentation

Decision making

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Decision making

  2. Last week Logical and boolean values Review Test Quiz

  3. This week Decision making Problem solving

  4. Decision making • Possibilities • if statement • if-else statement • if-else-if idiom • switch statement

  5. 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

  6. 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

  7. 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

  8. 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; }

  9. 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

  10. 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) );

  11. 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);

  12. 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

  13. 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; }

  14. 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; }

  15. 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

  16. 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) );

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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; } }

  32. 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; } }

  33. 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; } }

  34. 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; } }

  35. 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; } }

  36. 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; } }

  37. 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; } }

  38. 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; } }

  39. 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; } }

  40. 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

  41. 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?

  42. 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

  43. 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

  44. 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

  45. 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

  46. 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 ) {

More Related