• 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

Q: Assigning values in IF statement

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

I would like some guidance how to think about assigning values in an if statement. Please see the code below:

The output is:


b = true
FAILED: b = true && b
b = true || b
(b = true) && b



Now, what confuses me is line 12, why it evaluates to false?

Here is how I have understood how assigning inside an if should work. Let's look at line 6 for a moment. At first, b is false. Inside the if expression b is assigned the value true. After assignment IF condition checks b value and it evaluates to true.

If I think like that then in my mind line 12 should have succeeded also. Boolean b is initially false. When code reaches to if statement, then firstly b is set to true (like in line 6). After the assignment the left hand side of && evaluates to true and right hand side also. Condition should succeed, but it doesn't. What is ill in my logic?


 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, it's related to Operator precedence in java. Let me check it!
 
Innar Made
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, Abimaran Kugathasan!

Do you mean that && has higher precedence than b = true? Interesting thought. That would also comply why the if expression on line 26 evaluates to true (since it has parenthesis around b = true and thereby making it higher precedence than &&). But in that case what about the if condition on line 20, with the OR. If || has higher precedence than b = true, then both sides should be false also?
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java's Operator precedence will give you the idea.. Note that parentheses has higher precedence then other operators.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this coding. Absolutely, It'll drive you to solution!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are some facts to be considered. I think, No need of Operator precedence!

1) Short circuit AND operator checks the RHS, only if LHS is true.
2) Short circuit OR operator checks the RHS, only if LHS is false.



Here, b is kept in a stack for checking the condition before going into the condition block(if loop). In the if statement, LHS is true (we assigned it to true) but RHS is false(already b is kept in a stack). So the statement is false.



Here, according to 2nd statement, it works!

This is my Opinion. Please correct if wrong!
 
Innar Made
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Abimaran and Vijitha! I think I get it now.

Since && and || have higher precedence, they will be evaluated first.

Just my reflection of how I understand this now:

Here short-circuit && sees that b is false and fails the if expression. The boolean b inside the IF doesn't get assigned to true, since IF condition was failed by && already.


Here the short-circuit || sees that b is true and the IF condition succeeds right after that, b doesn't get assigned to true.

Thanks again
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course precedence matters.
Consider following two (from OP's example) cases:

And

Given that b is false initially in both cases It's the "&&" takes precedence over "=" hence the first expression evaluates to false. It's like saying:

In the second case it's obvious that it evaluates to true.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Innar Made wrote:
Since && and || have higher precedence, they will be evaluated first.



Correct

Innar Made wrote:Just my reflection of how I understand this now:

Here short-circuit && sees that b is false and fails the if expression. The boolean b inside the IF doesn't get assigned to true, since IF condition was failed by && already.



Incorrect. What happens is, due to precedence, this...

if ( b = true && b ) { }

Becomes this...

if ( b = (true && b) ) { }

The && is a short circuit operator, but there is nothing to short circuit. The left operand is true which requires it to evaluated the right operand. The result from the "&&" is false and is assigned to the b reference.


Innar Made wrote:

Here the short-circuit || sees that b is true and the IF condition succeeds right after that, b doesn't get assigned to true.



Not exactly sure what you mean by this description, so can't tell if you are correct or not. But here is what happens...

Due to precedence, this...

if ( b = false || b ) { }

Becomes this...

if ( b = (false || b) ) { }

The || is a short circuit operator, and as with previous, there is nothing to short circuit. The left operand is false which requires it to evaluated the right operand. The result from the "||" is true and is assigned to the b reference.

Henry
 
Innar Made
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Vijitha and Henry, for correcting me! Now I understand it. Gonna continue with next chapter of K&B book
 
reply
    Bookmark Topic Watch Topic
  • New Topic