This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Evaluate a boolean variable / differences Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Evaluate a boolean variable / differences" Watch "Evaluate a boolean variable / differences" New topic
Author

Evaluate a boolean variable / differences

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
1.
boolean x = true;
while (true);
....
2.
boolean x = true;
while (x == true);
....
3.
boolean x = true;
while (x=true);
....

What is the difference between this 3 expressions?
Thanks for answers.
Thomas.
Larry Jones
Greenhorn

Joined: Oct 22, 2002
Posts: 24
while(true)--This one would be an infinite loop. There is no expression that is going to evaluate. The only way to get out of this loop is a break statement.
while (x == true)--This is a boolean expression that will evaluate to either true or false based on the value of x. If x is true, the expression will evaluate to true. If x is false, the expression will evaluate to false.

while(x=true)--I am not completely sure about this one, but I think I got it. This is an assignment statement, not a comparison. So here you are assigning a value of true to x, and then x evaluates to true.
James Chegwidden
Author
Ranch Hand

Joined: Oct 06, 2002
Posts: 201

while(x=true)--I am not completely sure about this one, but I think I got it. This is an assignment statement, not a comparison. So here you are assigning a value of true to x, and then x evaluates to true.

Yep, you are assigning true to x and the evaluating the loop


Author and Instructor, my book
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Larry Jones:

while (x == true)--This is a boolean expression that will evaluate to either true or false based on the value of x. If x is true, the expression will evaluate to true. If x is false, the expression will evaluate to false.

So it has the same effect as
while (x) ...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
...and you have discovered that all expressions (including expressions that involve an assignment operator) create a return value. So, an assignment expression also represents some value.
[ November 12, 2002: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
James Chegwidden
Author
Ranch Hand

Joined: Oct 06, 2002
Posts: 201
Yep, that is why a C or C++ background can help.
Ruben Steins
Greenhorn

Joined: Nov 22, 2002
Posts: 15

This only works for booleans in Java afaik (as opposed to C/C++). In C and C++ the result of an assignment ( x=y ) will always be true if y is nonzero. In Java, the result of this expression is not a boolean (unless you're assigning a boolean value) and it will give a compile-time error.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Evaluate a boolean variable / differences
 
Similar Threads
2D Boolean array and randomness
Confusing Switch
Unreachable statement
Conditional Operators (try this)
Clarify...while....