What's the meaning of i++ ? 

What's the meaning of ++i ? 

int i = 6; 

i = i + 1; // 7 gets placed in i

Are these two fragments equivalent? 

  if (x == 5)    | if (x == 5)
    x = x + 1    |   x = x + 1
  else           | if (x != 5)
    x = 8        |   x = 8

No. In the first fragment x == 5 ? x = 6 : x = 8

In the second fragment x becomes 8 always. 

The second fragment is equivalent to x = 8; 

For equivalence in general: 

  (a) for all inputs we get the same outputs

  (b) also the sequence of internal states is identical 

int i = 5;

i += 1; // shortcut for i = i + 1; 

i *= 2; // also a shortcut for i = i * 2; 

i -= j; // same as i = i - j; 

i %= 5; // i = i % 5; 

Let's go back to i++ and ++i and I will describe the meaning. 

int i = 6; 

i is a valid expression (not a statement) with value, here: 6

i++ is a valid expression and statement with a value, here: 6 (and i becomes 7)

++i is also a valid expression (also statement), with a value, here: 7 (and i is 7)

So with this let me ask what does this mean/do:

  int i = 23;

  i = i++; // first get the 23 and then make i 24 then finish your
           // assignment and place 23 (which you got first) in i. Ouch. 

  // i is 23 at this stage 

  i = ++i; // first increment 23 to 24 inside i and get it, then 
           // finish your assignment and place 24 into i. Lucky us. 

From DrJava:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> int i = 6;
> i
6
> i++
6
> i
7
> ++i 
8
> i = i++
8
> i
8


Next let's solve this problem:

class One { 
  public static void main(String[] args) { 
    int x = 3, y = 5; 
    int b = x++ + y++ - ++y - ++x; 
    
    System.out.println( x + " " + y + " " + b ); 
  } 
}

Here's how we think about it: 

class One { 
  public static void main(String[] args) { 
    int x = 3, y = 5; 
    int b = x++ + y++ - ++y - ++x; 
    //      3 (and x becomes 4 right away)
    //          + 5 (and y becomes 6 right away)
    //      3   + 5   -   7 (and y is 7 as Neelan said)
    //      3   + 5   -   7 - 5 (and x is now still 5)
    System.out.println( x + " " + y + " " + b ); 
    //                  5         7        -4
  } 
}

We compile and run and this is confirmed. 

Now for attendance please go to

http://www.cs.indiana.edu/classes/c212-dgerman/sum2014/whatsnew.html

Choose one problem reconstruct it send me the code and the points. 

Attendance is just one problem. 

Practice for midterm says: solve them all. 

In lab you can solve Buffon Needle as a group.

You can also go over the remaining questions in:

https://www.cs.indiana.edu/classes/c212/sum2014/abcdef.pdf

--