Title: Decision making
1Decision making
2Last week
- Logical and boolean values
- Review
- Test
- Quiz
3This week
- Decision making
- Problem solving
4Decision making
- Possibilities
- if statement
- if-else statement
- if-else-if idiom
- switch statement
5Basic 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
6The 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
7Problem
- 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
8Determining 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
9Determining 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
10Determining 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) )
11Why 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)
12Problem
- 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
13Determining 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
14Determining 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
15Determining 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
16Determining 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) )
17Problem
- 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
18Serial 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
19Serial 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
20Serial 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
21Another 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
22Another 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
23Another 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
24Another 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
25Another 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
26Another 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
27Another 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
28Another 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
29Another 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
30Another 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
31Another 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
-
-
-
-
32Another 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
-
-
-
-
33Another 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
-
-
-
-
34Another 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
-
-
-
-
35Another 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
-
-
-
-
36Another 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
-
-
-
-
37Another 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
-
-
-
-
38Another 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
-
-
-
-
39Another 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
-
-
-
40Another 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
41Another 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?
42Another 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
43Another 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
44Another 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
45Problem
- 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
46Case 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 )