|
Conditions and Decisions
An Introduction with Examples in Java |
|
Prof. David Bernstein |
| Computer Science Department |
| bernstdh@jmu.edu |
switch Statement
if Statement
switch Statementswitch Statement (cont.)if Statementif Statement (cont.){}) if Statementsif Clauses:
else Clauses:
else clauseif Clausesif Clause:
if (value >= 0)
{
}
else
{
System.out.print("-");
}
if (value < 0)
{
System.out.print("-");
}
if Statementsresult = "Pass"; if (grade < 60) result = "Fail";
else Clause:
if (grade < 60) result = "Fail"; else result = "Pass";
if Statementsif Statementsif Statements (cont.)if Statements (cont.)if Statements (cont.)if Statements (cont.)
switch-Like Style if (seatLocationCode == 100) // case 100:
{
vip = true;
price = 40.0;
}
else if (seatLocationCode == 200) // case 200:
{
price = 30.0;
}
else if (seatLocationCode == 300) // case 300:
{
price = 15.0;
}
else // default:
{
price = 0.0;
}
boolean expression must be in
parentheseselse) are followed by a colonelif:
elif clause (that requires
a subsequent else clause)switch/match:
match statement
that is similar to the switch statement in Java
(without explicit break statements
and case _ used in place
of default)