• 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

Question about Non Short-Circuit logical operator question

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Hopefully I am asking this in the correct place. This is in relation to question 8 of Chapter 4 in the Sierra/Bates SCJP book.

there is an if statement that reads;

Boolean b2 = false;
"if((b2 = false) | (21%5) > 2) s += "x";

The question answers say that s never gets the "x" appended. I'm having trouble understanding how this actually works.
b2 is false so that renders TRUE on the left side of the "|". But but the test of 21%5 >2 renders FALSE.

So the way I read this since it is not a || being used here is that one is true so the test should be true. The logical operator is | and not an &.
What am I failing to understand about the Non Short-Circuit operator. I'm not able to figure out what is really happening here.

Thanks for any help you can provide.

 
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Davis...!!
It's not working the way you are thinking...

here when you write



it is replaced(understood) by compiler as



//e.g this will not work



i hope you understood this..
.
 
M Davis
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That really helps clear up a lot. Just to clarify something...

If the code actually read this way;


We would get a TRUE and a FALSE test then. How would that work out then for the if statement? It's not the & operator so both don't have to be true.

Would the if statement then be TRUE and the "x" be appended to s?
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes.



will have "x" appended to s because:

true OR false == true //second false IS NOT even looked at by the compiler
false OR true == true //second true IS looked at by the compiler

 
M Davis
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a bunch Bhaarat. That was what I thought. I sometimes overthink these things and wanted to clariy.

Thank you also ankur. You cleared some things up for me also.

 
ankur trapasiya
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bhaarat Sharma wrote:yes.



will have "x" appended to s because:

true OR false == true //second false IS NOT even looked at by the compiler
false OR true == true //second true IS looked at by the compiler


There is two versions of the OR:
| and || the second one is the short circuited, means it looks to the first operand if its true it will not check the second operand.
but | will always check both operands before move on.

Regards
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You shouldn't compare these operators in such a way. It implies they have similar meanings, while they practically have nothing in common.
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You shouldn't compare these operators in such a way. It implies they have similar meanings, while they practically have nothing in common.



Dear Stephan, I wrote what I wrote because Bhaarat Sharma is discussing the | funconality as if its || in the above discussion. but I like what you wrote , can you elaborate more so we can all binfit from this

Regards
 
reply
    Bookmark Topic Watch Topic
  • New Topic