Greetings. 

Yesterday we discussed the dangling else problem. 

In a nutshell it's similar to this: 

   (2 + 3) * 5 // without parens the expression changes value 

It's also similar to: 

   2 - (3 + 1) // again, if you remove the parens things change 

Let's remind ourselves the basics of the dangling else problem: 

  if (...) { // this branch contains just one statement: an if
    if (...) { 
      ...
    } // the else is missing
  } else {
    ... 
  } 

What keeps the else from being attached to the inner if? 

Answer: the curly braces. 

What are curly braces? 

Let's experiment in DrJava Interactions Panel: 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> 2 * 3
6
> a = 3
Static Error: Undefined name 'a'
> int a
> a = 6 * 4
24
> a
24
> a = a + 1
25
> a
25
> a == 25
true
> a % 13 
12
> a / 13
1
> a / 13.0
1.9230769230769231
> (double) a / 13
1.9230769230769231
>

Assume i is a variable of integer type (int). 

Assume i is 6. 

Please describe the meaning and value of the following expressions: 

                                       meaning         value 

                            i++   increment i by 1       i before being incremented 

                            ++i   increment i by 1       i after being incremented

Atendance: 

    Mark, Logan, Daniel, Jared, Nathan, Austin, Trevor

    Qin, Walt, Aleksa, Judy, Hallie, Adam, Brennan, Jacquelyn

    Nick, Yiming, Jingzhe, Zac, Paul, Morgan, Alex Ong, James

    Jon, Grant, M. Alex R., Mohan, Jack, William, Lauren

Question: if int i = 6; what is the value of i++ ?

Question: if int j = 4; what does code below print:

          j = j++ + j++; 
           // 4 and j becomes 5 
           // 4   + 5 and j becomes 6
         // = 9 so j becomes 9
          System.out.println( j ); // prints 9

Answer: see above. 

public class LectureFour {
  public static void main(String[] args) {
    int i = 6; 
    
    System.out.println( i++ ); // prints 6
  
    System.out.println( i ); // prints 7
  
  }
}

Another example: 


          int i = 6;

          int j = i++; 
               // 6 this is the value on the right hand side 
               // that value waits, to be used in the expression
               // meanwhile i becomes 7 and there's nothing more to compute
               // so we finally finish the assignment by putting 6 in j 

          System.out.println( i ); // 7 
          System.out.println( j ); // 6 



Conclusion: int i = 6; 
            i = i++; // i remains constant (potentially not what you wanted)

Question:

  If int i = 3, j = 5; what is i % j ?

  If int i = 8, j = -2; what is i + j ?

public class LectureFour {
  public static void main(String[] args) {
    { 
      int i = 6, j = 7; 
      System.out.println( i + j ); 
    }
    {
      int i = 3, j = -2; 
      System.out.println( i % j );     
    }
  }
}

public class LectureFour {
  public static void main(String[] args) {
    int n = 6;     
    while ( n++ < 16 ) {
      System.out.println("Howdy!"); 
    }
    System.out.println( n ); 
    
  }
}


public class LectureFour {
  public static void main(String[] args) {
    int a = 5, b = 15;
    a = a + 1; // first even number of interest
    while (a < b) ; {
      System.out.println( a ); 
      a = a + 2; 
    }
  }
}