# Increment operator

Monika Pasricha

Greenhorn

Posts: 19

posted 14 years ago

- 0

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

{

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

Posts: 19

Amit Pawar

Greenhorn

Posts: 3

posted 14 years ago

- 0

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!!

simple isn't it!!

kaffo lekan

Ranch Hand

Posts: 42

uma sakthi

Greenhorn

Posts: 11

Sowmya Vinay

Greenhorn

Posts: 24

posted 14 years ago

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!

- 0

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++));

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

Posts: 234

posted 14 years ago

- 0

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

Posts: 15

posted 14 years ago

- 0

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

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 |