Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

assignment operator with post increment

 
Ananth Majumdar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Precedence {

final public static void main(String args[]) {

int i = 0;

i = i++;

i = i++;

i = i++;

System.out.println(i); }

}
This should print 3 but it is printing 0. Please explain
[ March 27, 2007: Message edited by: Ananth Majumdar ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ananth


public class Precedence {

final public static void main(String args[]) {

int i = 0;

i = i++;

i = i++;

i = i++;

System.out.println(i); }

}


You are working with post increment operator which is incremented after the variable is used. Consider you are a assigning the i to itself. So each time i is getting zero. If you assign "i" to any other variable you will get the value of "i" what you expect "3"

int i =0;
i=i++; //i is assigned 0
i=i++; //i is assigned 0
i=i++; //i is assigned 0

System.out.println("i = " + i); //prints 0



Hope this helps you!

Thanks and Regards,
cmbhatt
 
Ananth Majumdar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i++ actually means that i=i+1; isn't it? So for i=i++

first
i=0; is assigned then i++ happens which is i=i+1; i.e i=1; so it should be 1. Why is it zero I am not able to understand. Please clarify..
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you independently write i++; it means i=i+1;

int i=0;
System.out.println("i = " + i++); // prints 0
System.out.println("i = " + i++); // prints 1
System.out.println("i = " + i++); // prints 2

System.out.println("i = " + i); //prints 3

// post increment only increments the value after the variable has been used.

What your case says : You are assigning the value of i++ to the same variable "i" so i stays 0 each time in assignment operation.

i++ does mean i=i+1; only and only when you keep it independent;
int i=0;
int j=i++; // j would be assigned 0 and then i would be i=i+1 //=1
j=i++; // j would be assigned 1 and then i would be i = i+1 //=2
j=i++; // j would be assigned 2 and then i would be i = i+1 //=3

What your case says:

int i=0;
i = i++; // assign 0 to i and operation completes
i = i++; // assign 0 to i and operation completes
i = i++; //assign 0 to i and operation completes
System.out.println("i = " + i);


Did you get that?

Thanks and Regards,
cmbhatt
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Chandra Bhatt

That means the i value will not increment ?
am i write
 
Ananth Majumdar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

int i=0;
i = i++; // assign 0 to i and operation completes
i = i++; // assign 0 to i and operation completes
i = i++; //assign 0 to i and operation completes


i=i++ // assign 0 to i and operation completes then when does i++ happen?
If the increment happens after the first i=i++ why is it not one atleast for the second assignment i=i++
 
Ananth Majumdar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when the expression completes why would i be incremented?


if i++ is not done at all then what is the difference between i=1; and i=i++;

What I supposed was even if it is not used for this assignment, it would be incremented by the time the variable is used in the next expression which means atleast it should be used in the second assignment. Please clarify
why i++ is not executed..
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i=i++;

Ok, let me explain it in detail


What happens with that code is this:

Let's say 'i' has a starting value of 0. The expression 'i++' evaluates to the pre-increment (original) value of 'i', or 0. The result of this expression (0) is remembered. The value of 'i' is then incremented ('i' is now 1). The '=' assignment operator has the least precedence, so it happens last: the result of the right-hand expression (what we remembered earlier, namely 0) is assigned back to 'i'. Therefore, the result of the whole statement is that 'i' has a value of 0.


Now, did you get this?

Thanks and Regards,
cmbhatt
 
Srinivas Kumar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sequence is as follows.
1. Left side variable i reads the value from the right side variable(i value 0).
2.Checks the end of expression and right side variable i gets incremented by Post increment operator(i=1) .
3. Left side variable is assigned with value it read before in step1 and hence overwritten with 0.
 
Ananth Majumdar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks chandra and kumar..I got it know..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic