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

Short-Circuit Logical Operators

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test
{
public static void main(String [] args)
{
int x= 0;
int y= 10;
for (int z = 0; z < 5; z++)
{
if (( y-- > 7 ) || (++x > 2))
{
y--;
}
}
System.out.println(y + " " + x);
}
}

Answer is 2 3

Can anyone explain output of this questions.It is from K&B Master Exam-1
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for (int z = 0; z < 5; z++)
{
if (( y-- > 7 ) || (++x > 2))
{
y--;
}
}

z=0:
(y-- > 7) is true and do y--; //x =0 y =8
z=1:
(y-- > 7) is true and do y--; //x =0 y =6
z=2:
(y-- > 7) is false and continue to evalute (++x > 2) which is false //x=1 y=5
z=3:
(y-- > 7) is false and continue to evalute (++x > 2) which is false //x=2 y=4
z=4:
(y-- > 7) is false and (++x > 2) is true then doing y--; //x=3 y=2
 
sonali sawant
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.Now i understood it.Thanks again.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic