[ACCEPTED]-What is the difference between while (x = false) and while (!x) in Java?-while-loop

Accepted answer
Score: 17

Note the difference between done = false and done == false. The first 5 one assigns done to be false and evaluates as false, the second 4 one compares done with false and is exactly identical 3 to !done.

So if you use:

while (done = false)
 // code here

Then done is set to false and the 2 code within the while loop doesn't run at 1 all.

Score: 4

The statement x = false is an assignment - you are 8 setting x to false. The statements x == false and 7 !x are the same, however. The statement x == false compares 6 x to false and will be true if the value 5 of x is false and false if the value of x is 4 true. The statement !x will result in the 3 negation of the value of x.

In your code, if 2 you replace while (!inputDone) with while(inputDone == false), you will get the expected 1 behavior.

Score: 3

You need to use == instead of = for comparisons.


Score: 2

The expression:

x = false

means assign x the value false.

After 6 this happens in your while(), it then evaluates 5 x, which is false so it doesn't enter the loop 4 at all.

On the other hand:

while (!x)

means "as long 3 as !x is true, continue entering the loop". since 2 !x means "the opposite of x". So as long 1 as x is false, the loop will continue

Score: 2

"while(done = false)" is equals to "done=false; while(done)"

It 3 should be written as "while(done == false)" or 2 "while(false == done)".

But still , !done 1 is the most readable code, it say "NOT DONE"

Score: 2

As many others have pointed out, you have 17 typoed ==. More interesting are the issues 16 surrounding this.

For language designed: Encouraging 15 side-effects in expressions is bad. Using 14 the symbol == to represent mathematical = is 13 not a good choice.

In terms of readability, !done reads 12 much better than done == false - we want "not done" (better, IMO, would 11 be "until done" instead of "while not done"). Although 10 (perpetual) newbies often write the redundant 9 someCondition == true.

It is a good idea to make variables final, although 8 clearly not feasible in this situation. However, we 7 can remove the flag entirely by using a 6 break statement. A minority opinions follows 5 a Single Entry Single Exit (SESE) rule, whereby 4 break is banned, whcih would make this example 3 more tricky (you'd need a test to see if 2 the text was a valid int, or in this case, move 1 the body into the loop.

Score: 1

Other answers have alluded to the fact that 2 writing x == false and x == true are bad style. There are 1 three reasons for this:

  • Conciseness: assuming that you are in a context where a Boolean is required, and "x" is a Boolean, it is less characters to write x than x == true, or !x than x == false.
  • Convention: seasoned programmers in Java (or C, C++, C# and most other languages) expect to see x rather than x == true, and !x rather than x == false.
  • Robustness: in Java the conditional and loop statements all require a Boolean valued expression in the condition. If y is not a Boolean, then a typo of the form if (y = foo) { will give a compilation error in Java. But if y is a Boolean then if (y = foo) { does not give a compilation error. Hence, by avoiding == for Booleans you avoid setting yourself up for a whole raft of bugs resulting from typos.
Score: 0

Many have already pointed out your misuse 4 of = vs. ==. I would like to point out that 3 running a static code analysis tool like 2 Findbugs would have found this for you right away.

See 1 QBA: Method assigns boolean literal in boolean expression

More Related questions