The almighty boolean toggle

August 5, 2008 at 10:53 PMAndre Loker

Do you the feeling when you know exactly what you mean, but you can't find a succinct way to express it? The guy that posted this classic piece of code to the forum surely does:

   1: if (flag == false)
   2:   flag = true;
   3: else if (flag == true)
   4:   flag = false;

Hint: flag = !flag

Sorry for not posting anything of greater importance. I'm rather tired :-)

Posted in: Snippets