Review:   the syntax rule for the statement inside   then-part   and   else-part   in the if-statement

  • Syntax of the two-way if-statement:

        if ( boolean-expression )
            then-statement ; // statement executed if boolean-expr is true
        else
            else-statement ; // statement executed if boolean-expr is false
    
     or:
    
        if ( boolean-expression )
        {   // then-part
            then-statement1; // statements executed if boolean-expr is true
            ...
        }
        else
        {    // else-part
            else-statement1; // statements executed if boolean-expr is false
            ...
        }
    

  • If there is no braces ( { ... }) after the keywords if and else, then:

    • The Java compiler will assume there is one statement in the then-part or else-part

Common mistake 1:   omitting the braces { ... } in the if-statement

  • Background information:

    • The Java compiler do not care about indentation

      (Java only follows the prescribed syntax rules !)

  • The syntax (= the way it is written) of the following if-statement:

     double radius = -20;
     double area = -9999;
    
     if ( radius >= 0 )
     {
        area = 3.14159 * radius * radius; // Statement 1
        System.out.println(area);         // Statement 2
     } 
    

    states:   the then-part contains both (= 2) statements

Because radius = -20, both statements in the then-part will be skipped

Common mistake 1:   omitting the braces { ... } in the if-statement

  • Background information:

    • The Java compiler do not care about indentation

      (Java only follows the prescribed syntax rules !)

  • A common mistake is omitting the braces:

     double radius = -20;
     double area = -9999;
    
     if ( radius >= 0 )
         
        area = 3.14159 * radius * radius; // Statement 1
        System.out.println(area);         // Statement not in the then-part
          
    

    Now:   the then-part contains only the first statement

DEMO: demo/03-selections/04-common-mistakes/CommonMistake1.java   (step with BlueJ)

Common mistake 2:   having a bogus ";" after the if-condition

  • Background information: the "empty" statement

    • The "empty" statement (i.e.: no statement) is written as follows:

                ; 
      

  • The statement in the then-part (or else-part) can be an "empty" statement:

      if ( radius >= 0 )
                    ;  

    Or written as follows:

      if ( radius >= 0 ) ; 

Common mistake 2:   having a bogus ";" after the if-condition

  • Correct program:

      radius = -20;
    
      if ( radius >= 0 )
      {
         area = 3.14159 * radius * radius; // Both statements are 
         System.out.println(area);         // inside the THEN part
      }  

  • Incorrect program:

      radius = -20;
    
      if ( radius >= 0 ) ; 
      {
         area = 3.14159 * radius * radius; // Both statements are
         System.out.println(area);         // outside the THEN part
      }  

DEMO: demo/03-selections/04-common-mistakes/CommonMistake2.java   (step with BlueJ)

Common mistake 3:   the dangling-else ambiguity

  • Background information:

    • The Java compiler will always match a keyword else with the nearest keyword if that preceeds it.

  • Problem:

    • Write a Java program that prints out the area of a circle only when 0 ≤ radius < 10 and prints "Illegal radius" otherwise

    Is this program correct ?

     double radius = 20;    // Also try radius = -20 
    
     if ( radius >= 0 )
        if ( radius < 10 )
           System.out.println( 3.14159*radius*radius);
     else
        System.out.println("Illegal radius"); 

DEMO: demo/03-selections/04-common-mistakes/CommonMistake3.java   (step with BlueJ)

Common mistake 3:   the dangling-else ambiguity

  • Explanation:   the else keyword is matched/paired up to the 2nd if keyword:

     double radius = 20;
    
     if ( radius >= 0 )
        if ( radius < 10 )
           System.out.println( 3.14159*radius*radius);
     else
        System.out.println("Illegal radius");

  • If you indent the program properly, the program reads as follows:

     double radius = 20;
    
     if ( radius >= 0 )
        if ( radius < 10 )
           System.out.println( 3.14159*radius*radius);
        else
           System.out.println("Illegal radius");

    That is why the program printed will "Illegal radius" for a legal radius 20

How to fix the dangling-else ambiguity

  • What if we want the else keyword to pair up to the 1st if keyword:

     double radius = -20;
    
     if ( radius >= 0 )
        if ( radius < 10 )
           System.out.println( 3.14159*radius*radius);
     else
        System.out.println("Illegal radius");

  • Solution:   use braces to force the association (i.e.: use syntax rules)

     double radius = -20;
    
     if ( radius >= 0 )
     {
        if ( radius < 10 )
           System.out.println( 3.14159*radius*radius);
     }
     else
        System.out.println("Illegal radius");

Common mistake 4:   testing floating point value for equality

  • Background information:

    • Calculations with double and float can have very small (rounding) errors

    Example:

    public class ShowFloatErr 
    {
       public static void main(String[] args) 
       {
          double x, y;
    
          x = 2.2 + 2.2 + 2.2 + 2.2 + 2.2 + 
              2.2 + 2.2 + 2.2 + 2.2 + 2.2;
          y = 22.0;
          
          System.out.println(x);   // Prints: 21.999999999999996 
          System.out.println(y);   // Prints: 22.0
       }
    } 

  • The computation (rounding) errors will cause the == and != test to fail

Common mistake 4:   testing floating point value for equality

  • Wrong way to test x == y:

    public class TestFloatEq1 
    {
       public static void main(String[] args) 
       {
          double x, y;
    
          x = 2.2 + 2.2 + 2.2 + 2.2 + 2.2 + 
              2.2 + 2.2 + 2.2 + 2.2 + 2.2;    // x = 21.999999999999996
          y = 22.0;                           // y = 22.0
    
          if ( x == y )
             System.out.println("x is equal to y");
          else
             System.out.println("x is NOT equal to y");
       }
    } 

  • Program will print "x is NOT equal to y"...

DEMO: demo/03-selections/04-common-mistakes/TestFloatEq1.java

Common mistake 4:   testing floating point value for equality

  • Correct way to test x == y:

    public class TestFloatEq1 
    {
       public static void main(String[] args) 
       {
          double x, y;
    
          x = 2.2 + 2.2 + 2.2 + 2.2 + 2.2 + 
              2.2 + 2.2 + 2.2 + 2.2 + 2.2;    // x = 21.999999999999996
          y = 22.0;                           // y = 22.0
    
          if ( Math.abs(x - y) < 0.0000001 )
             System.out.println("x is equal to y");
          else
             System.out.println("x is NOT equal to y");
       }
    } 

  • The method Math.abs( ) (in Java's Math class) returns the absolute value of its input.

DEMO: demo/03-selections/04-common-mistakes/TestFloatEq2.java