This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock Exam doubt increment expresion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mock Exam doubt increment expresion" Watch "Mock Exam doubt increment expresion" New topic
Author

Mock Exam doubt increment expresion

Jagdev Singh
Ranch Hand

Joined: Jan 12, 2010
Posts: 71

Hi all,
I have the following code.


The output is 1,0.

According to mine the output should be 1,1.

Can somebody please explain tricky logic at line 1.
Thanks in advance.
Will Myers
Ranch Hand

Joined: Aug 05, 2009
Posts: 319

at line 1 i is is assigned the value of 0 (before the increment) + 0 (which is what is returned from method f1)

so the call breaks down to
i = 0 + fi(1)

try changing the increment operator to the front ++i and it will return 1,1

have a look at what the increment operator does and when it does it....
Jagdev Singh
Ranch Hand

Joined: Jan 12, 2010
Posts: 71

Thanks Will,
One most doubt.
if I am using

Then the output is 1 which is expected.But when following code is used

The output is 0 .
According to me
In i = i++ first i is assigned to zero then the increment takes place but why this increment is not shown in print statement.Even though it is modifying same variable i.
Will Myers
Ranch Hand

Joined: Aug 05, 2009
Posts: 319

you don't need to do assignment so it should read:


prints 1

The reason the code prints 0 in your example is explained nicely here


Jagdev Singh
Ranch Hand

Joined: Jan 12, 2010
Posts: 71

Thanks Will,
I got it.It is really tricky.The given article is explaining this in best way.
 
 
subject: Mock Exam doubt increment expresion
 
Similar Threads
why output is 1,1 ?
Problem in understanding output...
value of i?
Why the result is 1,0
Why is this printing 1,0 instead of 1,1