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
StatementsSyntax
if
Statementsif
Clauses:
else
Clauses:
else
clauseif
Clausesif
Clause:
if (value >= 0) { } else { JMUConsole.print("-"); }
if (value < 0) { JMUConsole.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 Styleif (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; }
if
Statements
// One approach (that might not work) if (height < 58) idealWeight = 111; if (height < 59) idealWeight = 113; if (height < 60) idealWeight = 115; // Another approach (that might not work) if (height < 60) idealWeight = 115; if (height < 59) idealWeight = 113; if (height < 58) idealWeight = 111;
if
Statements (cont.)
// One approach (that might not work) if (height < 58) idealWeight = 111; if (height < 59) idealWeight = 113; if (height < 60) idealWeight = 115; // Still another approach (that might not work) if (height < 58) idealWeight = 111; else if (height < 59) idealWeight = 113; else if (height < 60) idealWeight = 115;