# Q: Assigning values in IF statement

Greenhorn
Posts: 17
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?

Abimaran Kugathasan
Ranch Hand
Posts: 2066
I think, it's related to Operator precedence in java. Let me check it!

Greenhorn
Posts: 17
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?

Vijitha Kumara
Bartender
Posts: 3918
10
Java's Operator precedence will give you the idea.. Note that parentheses has higher precedence then other operators.

Abimaran Kugathasan
Ranch Hand
Posts: 2066
Check this coding. Absolutely, It'll drive you to solution!

Abimaran Kugathasan
Ranch Hand
Posts: 2066
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!

Greenhorn
Posts: 17
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: 3918
10
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.

Henry Wong
author
Marshal
Posts: 21721
85
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.

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