• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Doubt In Kathy& Bates book questions at page 198

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The questions are as follows:

Class test{
public static void main(String arg[])
{
int x=0,y=0;
for(int z=0;z<5;z++)
{
if((++x>2)||(++y>2))
{
x++;
}
}
System.out.println(x + " " +y);
}

}

I traced it as follows

z=0- x=1,y=1;
z=1 x=2,y=2;
z=3 x=3 ,y=2 (Here x is >2 so x++ executed and now x is 4);
z=4 x=5 y=2(Here also x+ executed and x is 6 now)

So o/p should be x=6and y=2 but why it is mentioned in answers as

x=8 and y=2?


One more is

Class test{
public static void main(String arg[])
{
int x=0,y=0;
for(int z=0;z<5;z++)
{
if((++x>2)&&(++y>2))
{
x++;
}
}
System.out.println(x + " " +y);
}

}

I traced it as follows

z=0 x=1 y=0
z=1 x=2 y=0
z=2 x=3 y=1(the && condition is true so x++ evaluated and x is now 4)
z=3 x=5 y=2(here x++ excecuted and x is 6 now)
z=4 x=7 y=3

so o/p should be x=7 and y=3 right?
but it is mentioned as x=6 and y=3

where I go wrong?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try adding some println statements to show what the code is doing. For example...

[ July 19, 2007: Message edited by: marc weber ]
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mr vats rao

U missed one value of z. thats why you have got wrong answer

Analyse like

z=0-->x=1,y=1
z=1-->x=2,y=2
z=2-->x=3,y=2
once ++x ie ++2 becomes 3 xbecomes 4
x=4
z=3-->x=5,y=2
here in loop X becomes 6
z=4-->x=7,y=2
here in loop x becomes 8
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I traced it as follows

z=0 x=1 y=0
z=1 x=2 y=0
z=2 x=3 y=1(the && condition is true so x++ evaluated and x is now 4)
z=3 x=5 y=2(here x++ excecuted and x is 6 now)
z=4 x=7 y=3

so o/p should be x=7 and y=3 right?
but it is mentioned as x=6 and y=3



At z=2, you say that "&& condition is true", but the y>2 condition actually returns false for another two iterations of the loop. The if's block does not execute until z=4.
 
vatsalya rao
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for pointing out ,where I went wrong.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hail all!

It's a bit confusing this thing, but i got to the following conclusion:

z: 0 x: 1 y: 0 -> in the if condition, x is incremented
z: 1 x: 2 y: 0 -> again, the same thing
z: 2 x: 3 y: 1 -> x is incremented again, so the first part of the teste is true, making y also be incremented (just remembering here that y was not incremented before because && does not check the second statement if the first is false)
z: 3 x: 4 y: 2 -> same situation explained in the las step
z: 4 x: 6 y: 3 -> now we have the if scope executed so x is incremented 2 times.

Think its that!

Take care,
 
Paddy spent all of his days in the O'Furniture back yard with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic