aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q: Assigning values in IF statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q: Assigning values in IF statement" Watch "Q: Assigning values in IF statement" New topic
Author

Q: Assigning values in IF statement

Innar Made
Greenhorn

Joined: May 12, 2009
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

Joined: Nov 04, 2009
Posts: 2066

I think, it's related to Operator precedence in java. Let me check it!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Innar Made
Greenhorn

Joined: May 12, 2009
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

Joined: Mar 24, 2008
Posts: 3838

Java's Operator precedence will give you the idea.. Note that parentheses has higher precedence then other operators.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Nov 04, 2009
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!
Innar Made
Greenhorn

Joined: May 12, 2009
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

Joined: Mar 24, 2008
Posts: 3838

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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Innar Made
Greenhorn

Joined: May 12, 2009
Posts: 17
Thanks, Vijitha and Henry, for correcting me! Now I understand it. Gonna continue with next chapter of K&B book
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Q: Assigning values in IF statement