+ 1
In conditional statements or while loops:
For example, hasWon is a boolen that is set to false, "If (!hasWon)" should be thesame as "if (hasWon !=false)" But only the first one works, The second one is more understandable to me but it doesn't work,........ Please help..........
4 Antworten
+ 1
The way you wrote it they are not same. But you can write hasWon==false if you prefer. In a professional setting you might have to follow conventions, but for your own do as you like
0
If you have
boolean hasWon=false;
Then,
If (!hasWon) -> this will result to True because you're negating False.
And (hasWon !=false) ->will result to false because it wil be (false != false)
0
Thanks alot ,
I understand better now,
0
I feel the first should be more understandable 😊, but anyways; the two are not the same.
Since hasWon = false, !false (not false) will result to true, vice versa.
If you say
hasWon == false you'll get true as the result because the two sides of the equality are the same (false == false)
But when you say
hasWon != false you'll get false because false (hasWon) is not different from false.