4

I have a little doubt here,i have a code as

 int num=0;
 for(int i=0;i<5;i++){
   num=num++;
   System.out.print(num);
 }

why is the output always 00000

3
  • You have multiple syntax errors in your code? How does it run? int i notdefined, no semicolon after num++? Commented Aug 22, 2013 at 5:22
  • num=num++ where is ; ? Commented Aug 22, 2013 at 5:24
  • possible duplicate of Post increment operator not incrementing in for loop Commented Aug 22, 2013 at 5:42

7 Answers 7

10

The ++ operator increments num last, so when num is 0, you are setting it to 0 again.

It has to deal with how the ++ operator increments num, and what num is truly pointing to. To avoid it, just use num++

Interestingly enough, num=++num will correctly increment and assign the incremented value, though the whole purpose of the ++ operator, either pre or post, is that it modifies the value directly. You do not have to re-assign it back to the value.

Sign up to request clarification or add additional context in comments.

1 Comment

Good answer. 'Self-increment' operators don't have to be assigned to their variable, trying to do so is a mistake! +1.
3

use num++;

int num=0;
for(int i=0;i<5;i++){
   num++;
   System.out.print(num);
}

Output 12345

num=num++;

is equals to num=num;

num=++num;

is equals to num=num+1;

1 Comment

Op is asking for reason and you are providing the solutin
2
num=num++;

is equal -

num = num;
num ++;

First it assign then it try to increment the num which is already assigned. For better calrification -

 0  iconst_0
 1  istore_1 [num]
 2  iconst_0
 3  istore_2 [i]
 4  goto 22
 7  iload_1 [num] // Load first
 8  iinc 1 1 [num] // incement but no reload
11  istore_1 [num] // store old load value
12  getstatic java.lang.System.out : java.io.PrintStream [16]
15  iload_1 [num]
16  invokevirtual java.io.PrintStream.print(int) : void [22]
19  iinc 2 1 [i]
22  iload_2 [i]
23  iconst_5
24  if_icmplt 7
27  return

if we consider num=++num;

then generated byte code would be -

 0  iconst_0
 1  istore_1 [num]
 2  iconst_0
 3  istore_2 [i]
 4  goto 22
 7  iinc 1 1 [num] // Increment 
10  iload_1 [num] // load the incremented value
11  istore_1 [num] // store the loaded incremented value
...

2 Comments

Not strictly correct. The order in which assignments are executed is arbitrary (not defined by the language). I assume that in the poster's problem, the assignment (to the original value) was being executed after the post-increment. Thus overwriting any increment.
Exactly. The language does not define behaviour where assignments & increments are present, within the same statement. Neither do C/C++ (though there, the smallest unit of sequencing can be a comma-operator clause.) Corner-cases like this are left unspecified deliberately, to allow flexibility for optimization & discourage code from relying on highly-obscure/ or implementation-dependent outcomes.
1

num=num++;
Here you are using postfix ++ operator with assignment.So that means you assigned the value first and then incremented it.
So

 num = num++;

equivalent to

 num = num;//line1
 num+1;//line2

Deep look at line2 .The result of num+1 is not assigned to anything.So num is always have the value assigned at line1.i.e.,0 in your case.

Comments

1

Ok, In java = operator works as follows

L.H.S=R.H.S right hand side value will assign to left hand side variable.

In here initial value of num=0 and

num=num++ this increment not influence in place to num. if you do ++num it will effect at once in palace. so again you are assign 0 for num. So entire process this will happen continuously until loop stop.

Comments

1

That what postfix ++ does.

You can use:

num++;
System.out.print(num);

1 Comment

Has everyone lost their semicolons?
1

for explanation,

Post Increment(n++) : First execute the statement and then increase the value by one.

here, value of 'num++' is assigned to num and that is before increment and is 0. so num will have always value 0.

you can use simply num++ there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.