aspose file tools
The moose likes Beginning Java and the fly likes Problem with while statement Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Problem with while statement" Watch "Problem with while statement" New topic
Author

Problem with while statement

Rohan Deshmkh
Ranch Hand

Joined: Aug 31, 2012
Posts: 127
Why this code prints 3 and not enter into an infinite loop?


Doesn't the while(b=!b) always evaluates to true irrespective of the value of b?I thought assignment statement as a condition always evaluates to true.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16815
    
  19

Rohan Deshmkh wrote:Why this code prints 3 and not enter into an infinite loop?


Doesn't the while(b=!b) always evaluates to true irrespective of the value of b?I thought assignment statement as a condition always evaluates to true.


And that is where the flaw is ... see section 15.26 of the JLS.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.26

specifically ...

At run-time, the result of the assignment expression is the value of the variable after the assignment has occurred.


Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rohan Deshmkh
Ranch Hand

Joined: Aug 31, 2012
Posts: 127
Ok thanks , i got it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with while statement
 
Similar Threads
null instanceof aclass
multiple calls to ServletRequest.getWriter() ?
Q: Assigning values in IF statement
Unexpected output ??
what is this??? I m confused