programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Bear Bibeault
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• Tim Cooke
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Jj Roberts
• Carey Brown
Bartenders:
• salvin francis
• Frits Walraven
• Piet Souris

# output

Ranch Hand
Posts: 118
• Number of slices to send:
Optional 'thank-you' note:
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

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);
}
}

Ranch Hand
Posts: 35
• Number of slices to send:
Optional 'thank-you' note:
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.

Bartender
Posts: 6663
5
• Number of slices to send:
Optional 'thank-you' note:
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

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

Good luck with your preparation for the SCJP

sumaraghavi ragha
Ranch Hand
Posts: 118
• Number of slices to send:
Optional 'thank-you' note:
Thanks lot.I will most more clearly from next one

 This one time, at bandcamp, I had relations with a tiny ad. Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton