• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why the output is so?  RSS feed

 
sonu raj
Ranch Hand
Posts: 43
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Please Explain why the output is

90
90
90
90
90
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see our FAQ on this very topic.
 
sonu raj
Ranch Hand
Posts: 43
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please elaborate
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or not!
 
sonu raj
Ranch Hand
Posts: 43
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the compiler is showing no error and if I write i++ instead of i=i++ then the output is:
91
92
93
94
95

That I know its a post increment but why i=i++ give such output
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sonu raj wrote:Can you please elaborate

on what? I'm not sure how to explain it more clearly than what the article says. What - SPECIFICALLY - don't you understand about what it says?

Please remember that broad, general questions get broad, general answers.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sonu raj wrote:But the compiler is showing no error

Because there is no error. The compiler can only tell you if something is wrong with the syntax. It has no idea if your LOGIC is correct.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sonu raj wrote:Can you please elaborate

Which bit of the FAQ did you not understand ?
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sonu raj wrote:But the compiler is showing no error and if I write i++ instead of i=i++ then the output is:
91
92
93
94
95

That I know its a post increment but why i=i++ give such output


I didn't understand it correctly at first but "i++" is fully evaluated before the rest of the statement. Since its postfix the value of i++ is just i at first. Then it gets incremented, so in your example there is a brief period where i = 91, but then the statement is fully completed but the "i++" value of i is already locked in. So it goes back to being 90.
 
sonu raj
Ranch Hand
Posts: 43
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I am sorry fred that was the problem with my PC I saw

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors


But I didn't see that you have mentioned to see FAQ

I am really sorry
 
sonu raj
Ranch Hand
Posts: 43
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You everybody
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tyson Lindner wrote: . . . Since its postfix the value of i++ is just i at first. Then it gets incremented, . . .
Very subtle misunderstanding there. The value of i is incremented, not the value of i++. The value of i++remains the same as the old value of i and the = operator reassigns that value to i.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sonu raj wrote:I am really sorry

No harm, no foul.

Glad you were able to get the answer you needed.
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:see our FAQ on this very topic.

Appreciated this quite a bit and it taught me something new. Never gave much thought in the past as to exactly when i was actually incremented.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
The problem is that i is incremented first, but you do not see its value. You only see the value of i++, which is different.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!