File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

output

 
sumaraghavi ragha
Ranch Hand
Posts: 118
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sumaraghavi ragha
Ranch Hand
Posts: 118
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks lot.I will most more clearly from next one
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic