Decision making - PowerPoint PPT Presentation

About This Presentation
Title:

Decision making

Description:

Decision making Last week Logical and boolean values Review Test Quiz This week Decision making Problem solving Decision making Possibilities if statement if-else ... – PowerPoint PPT presentation

Number of Views:226
Avg rating:3.0/5.0
Slides: 47
Provided by: JimC117
Category:

less

Transcript and Presenter's Notes

Title: Decision making


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
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
The if-else statement
  • Syntax
  • if (Expression)
  • Action1else Action2
  • If Expression is true thenexecute Action1
    otherwiseexecute 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 lt v1 )
  • result v2
  • return result

9
Determining the minimum
Is v2 less than v1?
Yes it is. So update the guess with correct value
v2
v2 lt v1
false
true
No its 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 lt v1 )
  • result v2
  • return result

Consider Scanner stdin new Scanner(System.in)S
ystem.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 lt 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
Determining the minimum
  • Compare and choose smaller
  • public static int min (int v1, int v2)
  • int result
  • if ( v2 lt v1 )
  • result v2
  • else
  • result v1
  • return result

14
Determining the minimum
  • Compare and choose smaller
  • public static int min (int v1, int v2)
  • int result
  • if ( v2 lt v1 )
  • result v2
  • else
  • result v1
  • return result

15
Determining the minimum
Is v2 less than v1?
No its 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 lt v1 )
  • result v2
  • else
  • result v1
  • return result

Consider Scanner stdin new Scanner(System.in)S
ystem.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 lt v2 ) ( v1 lt v3 ) )
  • int result v1
  • if ( ( v2 lt v1 ) ( v2 lt v3 ) )
  • int result v2
  • if ( ( v3 lt v1) ( v3 lt 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 lt v2 ) ( v1 lt v3 ) )
  • result v1
  • if ( ( v2 lt v1 ) ( v2 lt v3 ) )
  • result v2
  • if ( ( v3 lt v1) ( v3 lt 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 lt v2 ) ( v1 lt v3 ) )
  • result v1
  • if ( ( v2 lt v1 ) ( v2 lt v3 ) )
  • result v2
  • if ( ( v3 lt v1) ( v3 lt 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 lt v2 ) ( v1 lt v3 ) )
  • return v1
  • if ( ( v2 lt v1 ) ( v2 lt v3 ) )
  • return v2
  • if ( ( v3 lt v1) ( v3 lt 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 lt v2 ) ( v1 lt v3 ) )
  • return v1
  • if ( ( v2 lt v1 ) ( v2 lt 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 lt v2 ) ( v1 lt v3 ) )
  • return v1
  • else
  • if ( ( v2 lt v1 ) ( v2 lt 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 lt v2 ) ( v1 lt v3 ) )
  • return v1
  • else
  • if ( ( v2 lt v1 ) ( v2 lt 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 lt v2 ) ( v1 lt v3 ) )
  • return v1
  • else
  • if ( ( v2 lt v1 ) ( v2 lt 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 lt v2 ) ( v1 lt v3 ) )
  • return v1
  • else
  • if ( ( v2 lt v1 ) ( v2 lt 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 lt v2 ) ( v1 lt v3 ) )
  • return v1
  • else
  • if ( ( v2 lt v1 ) ( v2 lt v3 ) )
  • return v2
  • else
  • return v3

Are these braces necessary? No! So lets 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 lt v2 ) ( v1 lt v3 ) )
  • return v1
  • else
  • if ( ( v2 lt v1 ) ( v2 lt 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 lt v2 ) ( v1 lt v3 ) )
  • return v1
  • else
  • if ( ( v2 lt v1 ) ( v2 lt 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 lt v2 ) ( v1 lt v3 ) )
  • return v1
  • else
  • if ( ( v2 lt v1 ) ( v2 lt v3 ) )
  • return v2
  • else
  • return v3

Does changing the whitespace affect the
solution? No! So lets 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 lt v2 ) ( v1 lt v3 ) )
  • return v1
  • else
  • if ( ( v2 lt v1 ) ( v2 lt 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 lt v2 ) ( v1 lt v3 ) )
  • return v1
  • else
  • if ( ( v2 lt v1 ) ( v2 lt 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 lt v2 ) ( v1 lt v3 ) )
  • return v1
  • else
  • if ( ( v2 lt v1 ) ( v2 lt 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 lt v2 ) ( v1 lt v3 ) )
  • return v1
  • else if ( ( v2 lt v1 ) ( v2 lt 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 lt v2 ) ( v1 lt v3 ) )
  • return v1
  • else if ( ( v2 lt v1 ) ( v2 lt 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 lt v2 ) ( v1 lt v3 ) )
  • return v1
  • else if ( ( v2 lt v1 ) ( v2 lt 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 lt v2 ) ( v1 lt v3 ) )
  • return v1
  • else if ( ( v2 lt v1 ) ( v2 lt 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 lt v2 ) ( v1 lt v3 ) )
  • return v1
  • else if ( ( v2 lt v1 ) ( v2 lt 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 lt v2 ) ( v1 lt v3 ) )
  • return v1
  • else if ( ( v2 lt v1 ) ( v2 lt 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 lt v2 ) ( v1 lt v3 ) )
  • return v1
  • else if ( ( v2 lt v1 ) ( v2 lt 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 lt v2 ) ( v1 lt v3 ) )
  • return v1
  • else if ( ( v2 lt v1 ) ( v2 lt v3 ) )
  • return v2
  • else
  • return v3

Do we really need the ( v2 lt v1 ) comparison?
42
Another serial implementation
  • Correct and compiles
  • public static int min (int v1, int v2, int v3)
  • if ( ( v1 lt v2 ) ( v1 lt v3 ) )
  • return v1
  • else if ( ( v2 lt v1 ) ( v2 lt v3 ) )
  • return v2
  • else
  • return v3

Do we really need the ( v2 lt v1 )
comparison? No. Its 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 lt v2 ) ( v1 lt v3 ) )
  • return v1
  • else if ( v2 lt v3 )
  • return v2
  • else
  • return v3

Do we really need the ( v2 lt v1 )
comparison? No. Its 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 lt v2 ) ( v2 lt v3 ) )
  • return v1
  • else if ( v2 lt v3 )
  • return v2
  • else
  • return v3

How many comparisons are made? Does the number
depend upon the values of v1, v2, and v3? Lets
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 lt v2 )
  • if ( v1 lt v3 )
  • return v1
  • else
  • return v3
  • else if ( v2 lt v3 )
  • return v2
  • else
  • return v3

else if ( v2 lt v3 )
Write a Comment
User Comments (0)
About PowerShow.com