If you have a boolean variable:
boolean myBool = true;
I could get the inverse of this with an if/else clause:
if (myBool == true)
myBool = false;
else
myBool = true;
Is there a more concise way to do this?
If you have a boolean variable:
boolean myBool = true;
I could get the inverse of this with an if/else clause:
if (myBool == true)
myBool = false;
else
myBool = true;
Is there a more concise way to do this?
Just assign using the logical NOT !
operator like you do in your condition statements (if
, for
, while
...). You're working with a boolean value already, so it'll change true
to false
(and vice versa):
myBool = !myBool;
An even cooler way:
myBool ^= true;
And by the way, don't use if (something == true)
, it's simpler if you just do if (something)
(the same with comparing with false, use the negation operator).