This week's giveaways are in the MongoDB and Jobs Discussion forums. We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line! See this thread and this one for details.
class Foozit { public static void main(string[] arg { Integer x = 0; Integer y = 0; for(Short z = 0;z < 5;z++) if((++x> 2)|| (++y >2)); x++; system.out.println(x + " " + y);
} } what was done? can somebody explain me why the result is 8 2 the iteration loop?what was done?
Tony Smith
Ranch Hand
Joined: Jul 07, 2007
Posts: 229
posted
0
I haven't gone through it in detail, but take a look at :
if((++x> 2)|| (++y >2));
notice the || short-cut OR operator? that means if the left side is true, right side is never evaluated. So when ++x>2, it will never go to ++y>2 part, so that kind of explain why y is ended up with just 2. So go over the code few times and write in paper and see what happens.
agustino felisberto
Greenhorn
Joined: Oct 16, 2007
Posts: 6
posted
0
yeah.this where is exactly my problem the left side.the right side i fully understand. why the left side stops at 8?
lets see x=0 y=0; x=1 y=1; x=2 y=2; x=3 if((++x>2)|| {++y >2)) true. i think it stops there with the third iteration of the loop,y is never touched again.
my question is why x==8 at end?not 10 or 12?or even x==3 and y==2?this exercice is in the book scjp(kathy sierra& bert bates).