• Post Reply Bookmark Topic Watch Topic
  • New Topic
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

output

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

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

Good luck with your preparation for the SCJP
 
sumaraghavi ragha
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks lot.I will most more clearly from next one
 
Don't play dumb with me! But you can try this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic