Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple code, but confusing

 
prasanna kanth
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



When the above code compile and run, it give 1,0 (I am confused how?)
when it pass the value its passing as 1 then the value should remain same when it returns zero.
and it should be like i = 1 + 0;

As per my idea it should give 1,1

Can any one explain clearly how it works here ?
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
prasanna kanth wrote:When the above code compile and run, it give 0,1 (I am confused how?)

Are you sure it gives 0,1. Or does it give 1, 0.
Did you actually run the code?
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
prasanna kanth wrote:
when it pass the value its passing as 1 then the value should remain same when it returns zero.
and it should be like i = 1 + 0;

In the code the incrementer is post increment......
 
prasanna kanth
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, it actually returns 1,0

Ok if its a Post increment, what is the value be passed to the function 0 or 1

 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code will be evaluated as
which will be evaluated as
Can you say what will be the output of
 
prasanna kanth
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the initial value of i = 0, then it will be 1 at the end.

I am still not clear (sorry to bug u), when it pass the value as 1, that means i is not zero its incremented to 1 :!: ?

If yes, then when it returns zero, the statement should be like i = 1 + 0 right :roll:

How come the incremented i value become again 0?

Cheers!
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This comes up often; lots of people get confused about what i++ is. And writing 0,1 when you mean 1,0 causes even more confusion!
This is a confusing bit of code, written to confuse, and difficult to read because you haven't indented it correctly.
And is that question from a book or website, in which case you ought to Quote Your Sources.

The value of f1(i) is 0. It always is, because there is return 0; in that method.
The value of i passed to f1 is 1, which you know because f1 prints 1,
The value of i before i++ is 0, and after i++ the value of i is 1. But there are two values there. There is the value of i, and the value of the subexpression, i++. They are different. The value of i++ is the old value of i, which is 0. So you are adding 0 and 0, getting 0, then re-assigning i to 0.
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
prasanna kanth wrote:If the initial value of i = 0, then it will be 1 at the end.

No the value will be still 0 at the end.
There was a very good post about how the value of i becomes from 0 to 1, then back to 0 w.r.t memory.
It really explained well, will try to search and give you the link.
But the bottom line, i will be unchanged(here 0).

So in our case i = i++ + f1(i);
First value of i is used for the expression(ie 0), then it is incremented(i becomes 1) and passed to the method f1 which returns 0.
so the expression becomes 0 + 0 = 0 which is finally assigned back to i.
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got the post ............ actually it was in the FAQ Post Increment Operator And Assignment
 
prasanna kanth
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Big thanks to all, finally understand how it woks, it was cooking my mind for last two days... woow
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome . It's nice when it works, isn't it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic