Decision Making
Selection structures
(if ....else and switch/case)
if ....else

We use selection many times every day as we
make decisions
For Example:
if the car is available
drive to work
Condition
Action
Q: what will happen if the car is not available?
A: In this case, an alternate action should be stated.
if ....else


if the car is available
drive to work
else
take public transit to work
The word else indicates the "take public
transit" is the alternate action that will take
place if the condition is NOT true. Only one
condition can be true.
else if

The words else and if are used together to introduce
another possible condition

Many conditions can be included but only one condition will
be true.

if the car is available
drive to work
else if public transit is running
take public transit to work
else if the bike is available
bike to work
else
walk to work
A Condition (boolean expression) can be
formed by using logical operator
Logical Operators consist of:
== Equals
> Greater than
!=
Not Equals
< Less than
<= Less than or equals
>= Greater than or equals
&& And
||
Or (the ‘pipe’ symbols)
And (&&)


Result is true if All Conditions are true
For example:
If you are hungry, AND you have
money, THEN you can buy lunch
&& (And)
Expression1
Expression2
Result
True
True
True
True
False
False
False
True
False
False
False
False
|| (OR)


Result is true if ANY condition is true.
For example:
If you have money OR you have a
credit card, you can pay the bill.
|| (Or)
Expression1
Expression2
Result
True
True
True
True
False
True
False
True
True
False
False
False
!= (Not)

Changes True Result to false & Vice
Versa.
For Example:
If not sunny then it is cloudy.

!= (Not )
Expression
Result
True
False
False
True
Selection structure in Java
Set brackets required
if more than 1
statement only
S im ple S e le ctio n:
if (a g e > 1 5 )
{ c .p rin tln ( “Y o u a re e lig ib le to d rive ” );
}
And ... both must be true
M u ltip le C o n ditio n s:
if (a g e > 1 8 & & re g istra tio n .e q u a ls Ig n o re C a s e ( “Y e s ” ) = = tru e )
{ c .p rin tln ( “Y o u ca n vo te ” );
c.p rin tln ( “E x e rcis e yo u r vo te !” );
}
Default Selection:
if ( mark >=50 )
{ c.println ( “You passed” );
}
else
{ c.println ( “You failed”);
Default ... ‘else’ occurs if no
other condition prior is true
Multiple Selection
M o s t lik e ly to o c c u r
if (m a rk > = 7 0 & & m a rk < 8 0 )
c .p rin tln ( “L e ve l 3 ... g o o d ”);
e lse if (m a rk > = 6 0 & & m a rk < 7 0 )
c .p rin tln ( “L e ve l 2 ... S a tis fa c to ry”);
S h o u ld b e
e lse if (m a rk > = 5 0 & & m a rk < 6 0 )
w ritte n in
c.p rin tln ( “L e ve l 1 ... P o o r”);
o rd e r o f
e lse if (m a rk > = 8 0 )
e x p e c te d
c.p rin tln ( “L e ve l 4 ... E xc e lle n t”);
o c c u ra n c e
e ls e
c.p rin tln ( “B e lo w L e ve l ... e x p e c ta tio n s n o t m e t”);
It is ru n ...
W h e n a m u ltip le s e le c tio n is ru n , th e p ro g ra m w ill lo o k fo r th e first
c o n d itio n th a t is tru e . A fte r it is fo u n d a n d th e s ta te m e n ts a re
e x e cu te d , th e re m a in in g c o n d itio n s a re ‘ju m p e d ’, a n d th e p ro g ra m
c o n tin u e s o n th e n e x t a va ila b le s ta te m e n t! T h e re fo re , th e o rd e r o f
th e c o n d itio n s a ffe c ts th e s p e e d th e p ro g ra m ru n s a t.
if without else


In some cases, there is an action to carry if a
condition is true and nothing to do if the
condition is not met. If a discount of 25% is
applied to all purchases over $200.00, the
calculation would be:
double discount=0;
if (purchase>200.00)
{
discount=purchase*.25;
purchase-=discount;
}
"switch/case" Multiple Selection Statement


Java provides the switch/case multiselection statements to perform different
actions based on the possible values of an
integer variable or expression.
Each action is associated with a constant
integral value (i.e. int, char, short or byte)
that the variable or expression can have. It
cannot be used with long or double.


This structure is used in conjunction
with the break instruction.
This type of structure is often referred
to in other languages as the select
…case structure.
Switch: Java Syntax
switch
{
( identifier)
case
case
case
case
case
default
value1:
// …statements to be executed
;
break
value2:
// …statements to be executed
;
break
value3:
value4:
value5:
// …statements to be executed…
;
break
:
// …statement
;
break
} // switch
Switch: Integer Example
// Here is an example of a switch construct that outputs a comment based on a
// student's
mark out of 10
int mark = In.getInt ( );
switch
{
(mark)
case 10:
System.out.println ("Perfect.");
break;
case 9:
case 8:
Case 7:
case 6:
case 5:
default:
System.out.println ("Pretty Good.");
break;
System.out.println ("Okay.");
break;
System.out.println ("Squeaked By!");
break;
System.out.println ("Did not pass. Ouch!");
break;
} // switch
Notes







The keywords if and else must be lower case
A line that contains a key word does not end with a semicolon.
The condition is in parentheses (age < 13)
By convention the action statements should be indented
Each set of actions is contained in braces, the braces may
be omitted if there is only one action but it is good
practice to include them.
The equality operator in Java is two equals signs. If you
are testing to see if a person's age is 21, the statement
would be:
if (age==21)
The switch/case construct must be used in conjunction
with the break statement. The default statement is to be
used as a catch all case in case none of the above cases
is executed.
Homework
Page 109 # 1
Page 115 # 4
Page 118 # 3
Read page 98, 99 (compareTo)
Descargar

Decision Making