JMU
Conditions and Decisions
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Review
Motivation
Two Conditional Statements in Java
The switch Statement
The switch Statement (cont.)

An Example

javaexamples/basics/SwitchExample.java (Fragment: 0)
       switch (seatLocationCode)
       {
          case 100:
             vip   = true;
             price = 40.0;
             break;
                
          case 200:
             price = 30.0;
             break;
                
          case 300:
             price = 15.0;
             break;
                
          default:
             price = 0.0;
             break;
       }
        
The if Statement
The if Statement (cont.)
A Railroad/Train Analogy for if Statements

Syntax

images/railroad_if-syntax.gif
Best Practices Involving if Statements
Eliminating Empty if Clauses
Defaults in if Statements
Styles of if Statements
Nested if Statements

The statement Can Be Another if

javaexamples/basics/IfExample2.java (Fragment: 0)
       if (income < 30000) 
       {
          level = "Low";
       }
       else
       {
          if (income < 60000) 
          {
             level = "Middle";
          }
          else
          {
             level = "High";
          }
       }

        
Nested if Statements (cont.)

A Condensed Style for "Nesting"

javaexamples/basics/IfExample2.java (Fragment: 1)
       if      (income < 30000) level = "Low";
       else if (income < 60000) level = "Middle";
       else                     level = "High";

        
Nested if Statements (cont.)

Multiple Nested if Statements

javaexamples/basics/IfExample3.java (Fragment: 1)
       if (years <= 1)
       {
          title = "Freshman";
       }
       else
       {
          if  (years <= 2) 
          {
             title = "Sophomore";
          }
          else 
          {
             if  (years <= 3) 
             {
                title = "Junior";
             }
             else 
             {
                title = "Senior";
             }
          }
       }

        
Nested if Statements (cont.)

A Condensed Style for Multiple Nested ifs

javaexamples/basics/IfExample3.java (Fragment: 0)
       if (years <= 1) title = "Freshman";
       else if  (years <= 2) title = "Sophomore";
       else if  (years <= 3) title = "Junior";
       else title = "Senior";
        
        
Nested if Statements (cont.)

A switch-Like Style

javaexamples/basics/IfExample4.java (Fragment: 0)
       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;
       }
        
Common Logic Errors Involving Sequential if Statements

Is either or both of the following correct?

javaexamples/basics/IfExample5.java (Fragment: 0)
       //    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;

        
Common Logic Errors Involving Sequential if Statements (cont.)

Is either or both of the following correct?

javaexamples/basics/IfExample5.java (Fragment: 1)
       //    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;