How do you use an increment operator for booleans so every time it adds ++, it changes from true to false or from false to true?
Asked
Active
Viewed 70 times
0
-
6Are you referring to toggling? `boolean = !boolean` – Jacob G. Jun 18 '18 at 03:51
-
1ah yes. thank you. Sorry for the ignorance. – Allen Rhodes Jun 18 '18 at 03:52
-
Or `boolean ^= true` to us a XOR. I find that elegant – AxelH Jun 18 '18 at 05:51
1 Answers
1
There is no way to toggle a boolean with ++
operator as those are generally applied on numbers. In case you want to toggle your boolean from false to true and true to false. U can use inversion operator
e.g. myBoolean = !myBoolean;

Sagar Kharab
- 369
- 2
- 18
-
How do you use this inversion when using getter and setter methods? – Allen Rhodes Jun 18 '18 at 05:31
-
please share the your code where you want this. I ll be happy to help. – Sagar Kharab Jun 18 '18 at 05:44
-