aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "output" Watch "output" New topic
Author

output

sumaraghavi ragha
Ranch Hand

Joined: Nov 17, 2006
Posts: 118
why the value y is 2 hear?
why can't it is 1 becauese the vale of x is 1 in the 1st time

then it is 3 so the value of y is not incermented so the output should be

8 1

why it is 8 2please expalin me

Thanks in advance

class Foozit {
public static void main(String[] args) {
Integer x = 0;
Integer y = 0;
for(Short z = 0; z < 5; z++)
if((++x > 2) || (++y > 2))
x++;
System.out.println(x + " " + y);
}
}
Noam Wolf
Ranch Hand

Joined: Jan 12, 2008
Posts: 35
Hi sumaraghavi,

First of all I believe you're supposed to quote your sources (I believe this is from K&B).

Second the value of x is incremented twice in the for loop IF ++x>2 or ++y>2, once AFTER the "if" condition has executed and then once more after x++ has executed.

So let's look at the results for x, y, and z after each iteration of the for loop.

iteration z x x++ y
1 0 1 (1 not >2) no call 1
2 1 2 (2 not >2) no call 2
3 2 3 (3 is >2) 4 (call) 2 (no change)
4 3 5 (5 is >2) 6 (call) 2 (no change)
5 4 7 (7 is >2) 8 (call) 2 (no change)
6 5 - not less than 5 for loop ends.

I think what you're missing here is that the value of x does NOT get incremented twice for the first 2 iterations because both cases of the if block are not true and therefore x++ is not reached.

I hope that helps.


because .net guys can also write in java
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

sumaraghavi please try to use different and more meaningful subject lines as it may confuse those who want to reply to your posts. For example the following post looked like a double post because you used the same heading

http://www.coderanch.com/t/269135/java-programmer-SCJP/certification/output

Good luck with your preparation for the SCJP


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
sumaraghavi ragha
Ranch Hand

Joined: Nov 17, 2006
Posts: 118
Thanks lot.I will most more clearly from next one
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: output