Given that b is a boolean variable, are the expressions (!b) and (b==false) the same?
Here’s where I’m at so far with this question:
-
!b — returns a [FALSE response if b is true] or a [TRUE response if
b is false]. -
Declaring a boolean variable, ‘b’, in this case, would set it to
true, therefore making !b return false.
I suppose all I need to complete the question is to know whether or not b is initially true, which I assume it to be?
Hopefully this makes sense and someone can clear this up for me?
EDIT:
This is a question from my tutor. I am asked to decide if this statement is true or false:
“The expressions (!b) and (b==false) are equivilaent, where b is a boolean variable”
Here is here extremelly helpful cryptic clue when I asked for some help:
The situation with !b is that these expressions should provide the same result for all values of b in order for the statement to be correct. Ie if b is false, then both expressions give the same result and if b is true then both expressions give the same result. You need to look at both expressions.
Yes, they are equivalent in “sane” languages 🙂 (let’s say Java, C++, C#)
In Javascript if
bcan only betrueorfalse, it is correct (but beware!if (!null)is true,if (null == false)is false. The same forundefined)Be aware that not all languages use
!forNOTand==forEQUALS. For example in C you would probably useintinstead ofboolandFALSEinstead offalse, but the result would be the same (but note that in C, non-zero is “true”)