• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Short-circuit operators

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can someone please explain me this code....according to me it should print both the statements.
As && is true if both the operands are true and will be evaluated first as per precedence order.
And | | is true if either or both the operands are true.
int l = 4;
int m =14;
if(l > 1 && m < 6 | | m > 0 )
System.out.println("Prints");
if(l > 0 && m < 5 | | m >50 )
System.out.println("Doesn't Print");
Thanks.
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try using parenthesis around it so it knows which way you want it to read if((...&&...)| |....)
 
Mukti Bajaj
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fred,
I have revised using parenthesis, but still it gives the same result....can you please expalin me why doesn't it prints both the outputs.
int l = 4;
int m =14;
if((l > 1 && m < 6) | | m > 0 )
//&& gives the result true and | | is also true ...both operands
System.out.println("Prints");
if((l > 0 && m < 5) | | m >50 )
//&& gives the result true and | | is also true...one operand
System.out.println("Doesn't Print");
Thanks.
[This message has been edited by Mukti Bajaj (edited January 10, 2001).]
 
Fred Abbot
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what output are you getting?
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Mukti Bajaj:
Hi,
Can someone please explain me this code....according to me it should print both the statements.
As && is true if both the operands are true and will be evaluated first as per precedence order.
And | | is true if either or both the operands are true.
int l = 4;
int m =14;
if(l > 1 && m < 6 | | m > 0 )
System.out.println("Prints");
if(l > 0 && m < 5 | | m >50 )
System.out.println("Doesn't Print");
Thanks.



Hi Mukti,
I ran your program-->It prints 'Print'.
if(l > 1 && m < 6 | | m > 0 )
System.out.println("Prints");
When you run the above if condition, first l>1 is tested and is found to be true. So m>6 is evaluated and is found false. So (l>1 && m<6) is evaluted to false. After that m>0 is evaluated to true. This boolean value is OR'ed with the previous false value and the ultimate result is true.
So your if condition evalutes to true, and the out put is printed 'Print'.
Similarly for the second if condition.
if(l > 0 && m < 5 | | m >50 )
l>0 && m<5 evalutes to false and m>50 also evaluates to false. So your if condition is evaluted to false and it does no output 'Does'nt Print'
As Fred suggested it would be better if you used parenthesis to make the code clearer.
And as such, in your case all the relational conditions are evaluated. The code is not short circuited any where. You could have used & and | to obtain the same result.
SJ

[This message has been edited by Sajan Joseph (edited January 10, 2001).]
 
Mukti Bajaj
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sajan,
Thanks....i got it now.
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sajan,
Your explanation was really cool...
Kajol
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!