File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Increment operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Increment operator" Watch "Increment operator" New topic
Author

Increment operator

Monika Pasricha
Greenhorn

Joined: Jan 08, 2001
Posts: 19
class ex
{
public static void main(String a[]) {
int i=1;
System.out.println(i++ + i++);
System.out.println(((i++) + (i++)));
System.out.println((i++) + (i++));
}
}
The output is 3, 7, 11.
First output is clear.
Please clarify last two outputs.
Thanx
Monika Pasricha
Greenhorn

Joined: Jan 08, 2001
Posts: 19
I got the answer.. The reason is same as for the first output.
Amit Pawar
Greenhorn

Joined: Jan 17, 2001
Posts: 3
The value of i after first SOP statement becomes 2.In second SOP statement as precedence is given to the (),inside SOP statement (i++) sets i =3 and then another (i++) sets it to 4.therefore 3+4=7.In third SOP the same thing happens as in second SOP, hence 5+6 = 11
simple isn't it!!
kaffo lekan
Ranch Hand

Joined: Feb 13, 2001
Posts: 42

Hello Pawar,
i still dont understand the operation, please break down the explanation futher.
Thanks
kaffo
uma sakthi
Greenhorn

Joined: Feb 02, 2001
Posts: 11
The split up is as follows
System.out.println(1 + 2)
i=3
System.out.println(((3) + (4)))
i=5
System.out.println((5) + (6))
i=7
}
}
The output is 3, 7, 11.
Thanx
uma
Sowmya Vinay
Greenhorn

Joined: Feb 01, 2001
Posts: 24
Originally posted by kaffo lekan:

Hello Pawar,
i still dont understand the operation, please break down the explanation futher.
Thanks
kaffo


1.System.out.println(i++ + i++);
2.System.out.println(((i++) + (i++)));
3.System.out.println((i++) + (i++));

initial value of i=1;
Consider step 1:
Let me write i++ + i++ as a + b where a=i++ before the binary + operator and b=i++ after the binary + operator.
Since a post increment is being done,the value of a is 1, value of i is i+1 i.e 2. Now the current value of i is 2, so the value of b is i which is 2 and the value of i is incremented to 3.
So final result after step 1 is:
a=1, b=2, i=3
So the results is printed as (a+b)=(1+2)=3
Write the expression in step 2, again in terms of a and b. Then
the expression would be (a+b).
Again by appliying the same procdeure as above, the value of a is current value of i which is 3 , the value of i after this post increment is 4, the value of b is current value of i, which is 4 and the value of i after this increment is 5.
So the final values are a=3,b=4 and i=5, giing a result of a+b=3+4=7.
Similarly, by applying the same procedure to 3,value of a is current value of i which is 5, i is incremented to 6, value of b is current value of i which is 6, i is incremented to 7.
Final result is a+b=5+6 = 11.
So, the algorithm can be summarised as:
1)Let a=i++ and b=i++
2) The evaluation goes as follows, a=i, i=i+1, b=i, i=i+1,a+b(from left to right).Note that the paranthesis is steps 2, and 3 are redundant and are of no significance in this case!
Hope this helps!

sona gold
Ranch Hand

Joined: Feb 14, 2001
Posts: 234
Originally posted by Monika Pasricha:
[B]class ex
{
public static void main(String a[]) {
int i=1;
System.out.println(i++ + i++);

i ++ = 1 (means the value of i and pass ++ i.e 1 and pass 2)
i ++ = 2 (means the value of i and pass ++ i.e. 2 and pass 3)
so te result of first statement is 3 (i is 2 here)

System.out.println(((i++) + (i++)));
(i++) = 3 (means the value of i and ++ (2++) = (3))
(i++) = 4 (means the value of i and ++ (3++) = (4))
so the result here is 3 + 4 = 7 ( i is 4 here)
System.out.println((i++) + (i++));
(i++) = 5 (means the value of i and ++ (4++) = (5))
(i++) = 6 (means the value of i and ++ (5++) = (6))
so the result here is 5 + 6 = 11
}
}
The output is 3, 7, 11.


sona<br />SCJP
venkatesan Rajagopalan
Greenhorn

Joined: Jan 12, 2001
Posts: 15
the simplest way to explain is this for post increment operations:
The increment works as follows:
The first one:
sop(1(2) + 2(3))
total is 3 but the value of i is 3
second one:
sop(3(4) + 4(5))
total is 7 and the value of i is 7
third one:
sop(5(6) + 6(7))
total is 11 and the value of i 7
Trust this sequencing should help
venkat
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Increment operator